JTA: Hibernate can't find java:jboss/UserTransaction
spadhi Oct 28, 2014 4:14 AMI'm migrating a legacy Spring3/Hibernate3/JBoss5 codebase to Spring4/Hibernate4 on JBoss Wildfly8. Getting Spring4 and Hibernate4 to play well together using JTA on Wildfly has been a challenge so far. The issues that I'm facing are:
1. Hibernate is unable to get the transaction. The error happens in line 92 of hibernate-orm/JTASessionContext.java at master · hibernate/hibernate-orm · GitHub
2. To get around the issue#1, I open a new Hibernate session (when getSessionFactory().getCurrentSession() throws the exception above). Hibernate is then unable to locate the userTransaction at line 79 of hibernate-orm/JtaTransactionFactory.java at master · hibernate/hibernate-orm · GitHub
The JNDI view in the Wildfly console shows java:jboss/TransactionManager and java:jboss/UserTransaction as follows:
java:jboss |
TransactionManager | TransactionManagerDelegate@49e6e9c8 |
TransactionSynchronizationRegistry | TransactionSynchronizationRegistryImple@40cd0746 |
UserTransaction | UserTransaction |
jaas | java:jboss/jaas/ Context proxy |
The Spring setup "seems" to be ok based on initial debugging. And the Hibernate session seems not to be in sync. Not sure what I'm missing here, so I would really appreciate it if anyone has tried this before and has any suggestions. Here's the relevant snippet of what I have in the Spring config xml:
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransactionName" value="java:jboss/UserTransaction"/>
<property name="transactionManagerName" value="java:jboss/TransactionManager"/>
</bean>
<tx:annotation-driven/>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="false">
<property name="jndiName" value="java:jboss/datasources/jdbc/my-srvr"/>
<property name="cache">
<value>false</value>
</property>
<property name="proxyInterface">
<value>javax.sql.DataSource</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="true">
<property name="dataSource" ref="dataSource"/>
<property name="jtaTransactionManager" ref="transactionManager"/>
<property name="packagesToScan" value="com.mydomain" />
<property name="mappingResources">
<list>
<value>sample1.hbm.xml</value>
<value>sample2.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<!-- JTA -->
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</prop>
<prop key="hibernate.flushMode">AUTO</prop>
<prop key="jta.UserTransaction">java:jboss/UserTransaction</prop>
<prop key="jta.TransactionManager">java:jboss/TransactionManager</prop>
<prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform</prop>
<prop key="hibernate.current_session_context_class">org.hibernate.context.internal.JTASessionContext</prop>
<!--Turned off caching to focus on JTA issues-->
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<!--prop key="hibernate.cache.provider_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop-->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">sample-ehcache.xml</prop>
</props>
</property>
<!--No hibernate4 equivalent in Spring; comment out for now-->
<!--property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property-->
</bean>
Thanks in advance!