UserTransaction: javax.naming.NameNotFoundExceptio

2020-04-21 02:05发布

问题:

After migrating to Java 8 and WildFly 8.2.0 I get this error on every Transaction

UserTransaction: javax.naming.NameNotFoundException: UserTransaction [Root exception is java.lang.IllegalStateException: JBAS014237: 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:140) at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:81) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:202) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:179) at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:235) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:188) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184) at javax.naming.InitialContext.lookup(InitialContext.java:417) [rt.jar:1.8.0_45] at javax.naming.InitialContext.lookup(InitialContext.java:417) [rt.jar:1.8.0_45]

The Bean has the annotation @TransactionManagement(TransactionManagementType.BEAN)

This is where the exception happens:

final javax.naming.Context tmpNamingContext = new javax.naming.InitialContext();
                    userTransaction = ((UserTransaction) tmpNamingContext.lookup("java:jboss/UserTransaction"));

I don't know what is happening here and cannot found any good solution.

Please help.

回答1:

Get the UserTransaction with the annotation @Resource instead of through the JNDI Name was the solution. UserTransaction is no more in WildFly 8.2.0, in JBoss it was set already.

This solves the issue.