从 JBoss 7 迁移到 WildFly 9 时使用 CMT 的 EJB

EJB with CMT when migrate from JBoss 7 to WildFly 9

我正在将我的应用程序从 JBoss 7 迁移到 WildFly (v9.0.1),但由于 bean 事务管理错误而未部署。

    Caused by: javax.naming.NamingException: WFLYNAM0062: Failed to lookup env/com.component.eventmgt.EventServiceImpl/transaction [Root exception is java.lang.RuntimeException: WFLYNAM0059: Resource lookup for injection failed: java:jboss/UserTransaction]
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:157)
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:83)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
    at org.jboss.as.naming.deployment.ContextNames$BindInfo.getReference(ContextNames.java:316)
    ... 90 more
Caused by: java.lang.RuntimeException: WFLYNAM0059: Resource lookup for injection failed: java:jboss/UserTransaction
    at org.jboss.as.naming.deployment.ContextNames$BindInfo.getReference(ContextNames.java:319)
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:143)
    ... 95 more
Caused by: javax.naming.NameNotFoundException: UserTransaction [Root exception is java.lang.IllegalStateException: WFLYEJB0137: Only session and message-driven beans with bean-managed transaction demarcation are allowed to access UserTransaction]
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:153)
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:83)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
    at org.jboss.as.naming.deployment.ContextNames$BindInfo.getReference(ContextNames.java:316)
    ... 96 more

这是 EventServiceImpl class。

    @Stateless
    @Remote(EventService.class)
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public class EventServiceImpl implements EventService {

        /**
         * Logger
         */
        private static Logger log = LoggerFactory.getLogger(EventService.class);

        private EventTableDAO eventDao;

        @PersistenceContext(unitName = "SOMF-GT")
        private EntityManager entityManager;

        @Resource
        private UserTransaction transaction;

       public List<Map> loadEvents() throws EventsException {

        Configuration configurationEntry = new Configuration();
        try {
            Map configuration = configurationService.getConfiguration();
            if (configuration != null) {

        eventDao = new EventTableDAO(Event.class, entityManager, transaction);
        List<Map> eventsMapList = new ArrayList();
}
}

我知道如果我使用 @TransactionManagement(TransactionManagementType.BEAN) 将事务管理更改为 BMT,但会出现以下错误

WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)

我想知道为什么我们必须首先更改它?

任何信息,请!

这些更改在 Wildfly 8 中推出,并且(如下所述)基于 EJB 3.1 中全局 JNDI 命名空间的标准化。

来自野蝇 8 Developer Guide:

EJB 3.1 introduced a standardized global JNDI namespace and a series of related namespaces that map to the various scopes of a Java EE application. The three JNDI namespaces used for portable JNDI lookups are java:global, java:module, and java:app. If you use JNDI lookups in your application, you will need to change them to follow the new standardized JNDI namespace convention.

To conform to the new portable JNDI namespace rules, you will need to review the JNDI namespace rules and modify the application code to follow these rules.

指南进一步说明:

WildFly 8 has tightened up on JNDI namespace names to provide predictable and consistent rules for every name bound in the application server and to prevent future compatibility issues. This means you might run into issues with the current namespaces in your application if they don't follow the new rules.

这是 table 的片段,显示 Examples of JNDI mappings in previous releases and how they might look now 特定于 UserTransaction

Previous Namespace          New Namespaces
------------------          --------------
java:comp/UserTransaction   java:comp/UserTransaction (This will not be accessible for non EE threads, e.g. Threads your application directly creates)
java:comp/UserTransaction   java:jboss/UserTransaction (Globally accessible, use this if java:comp/UserTransaction is not available)

编辑回复:WFLYEJB0137:

这是理论技巧,可能毫无价值 - 让我知道,我会删除它。 Java EE 6 Tutorial - Container-Managed Transactions 说:

Enterprise beans that use container-managed transaction demarcation also must not use the javax.transaction.UserTransaction interface.

进一步:

(Transaction) Required Attribute

If the client is running within a transaction and invokes the enterprise bean’s method, the method executes within the client’s transaction. If the client is not associated with a transaction, the container starts a new transaction before running the method.

The Required attribute is the implicit transaction attribute for all enterprise bean methods running with container-managed transaction demarcation. You typically do not set the Required attribute unless you need to override another transaction attribute. Because transaction attributes are declarative, you can easily change them later.

异常消息几乎说明了一切:

WFLYEJB0137: Only session and message-driven beans with bean-managed transaction demarcation are allowed to access UserTransaction

您的 EJB 正在使用容器管理事务 (CMT) 划分,它不与 UserTransaction 所在的 bean 管理事务 (BMT) 划分互操作。

关于切换到 BMT 和

WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)

我发现 这似乎表明交易将由您管理,正如您在评论中指出的那样@Marco。看来你做了适当的修改。