0

I have tried many different solutions, but getting the exception:

org.hibernate.HibernateException: Unable to locate current JTA transaction

I am using Atomikos, Hibernate 4/5, Spring 4 and Jetty.

How to configure Spring 4 + Hibernate 4/5 (non JPA)?

Atomikos documentation only has an example with JPA.

I would be very grateful for a working example (non JPA, non JEE Application server).

This example changes non JPA:

<context:annotation-config />
  <context:component-scan base-package="com.atomikos.icatch.jta.hibernate4" />
  <tx:annotation-driven transaction-manager="transactionManager"  mode="proxy" proxy-target-class="false" />
  <aop:aspectj-autoproxy />
  <!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
  <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" depends-on="dataSource">
    <property name="forceShutdown" value="true" />
  </bean>

  <!-- Also use Atomikos UserTransactionImp, needed to configure Spring -->
  <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout" value="300" />
  </bean>

   <!-- by default : looks for java:comp/UserTransaction -->
   <bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
     <property name="userTransaction" ref="atomikosUserTransaction"></property>
     <property name="transactionManager" ref="atomikosTransactionManager"></property>
   </bean>

   <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
    destroy-method="close" init-method="init" >
     <property name="uniqueResourceName" value="atomikos-standalone" />
     <property name="maxPoolSize" value="10" />
     <property name="minPoolSize" value="5" />
     <property name="testQuery" value="SELECT 1" />
     <property name="xaDataSource" ref="xaReferent" />
   </bean>

   <bean id="xaReferent" class="org.h2.jdbcx.JdbcDataSource">
     <property name="URL" value="jdbc:h2:~/test-db;MODE=PostgreSQL;MVCC=TRUE;DB_CLOSE_DELAY=-1" />
     <property name="user" value="sa" />
     <property name="password" value="" />
   </bean>

   <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

   <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />        
     <property name="packagesToScan" value="com.atomikos.icatch.jta.hibernate4"/>                
     <property name="jtaTransactionManager" ref="transactionManager" />
     <property name="hibernateProperties">
       <props>                
         <prop key="hibernate.transaction.jta.platform">com.atomikos.icatch.jta.hibernate4.AtomikosPlatform</prop>                                              
         <prop key="show_sql" >true</prop>                                 
       </props>
     </property>                
   </bean>
 </beans>

Update

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />        
  <property name="packagesToScan" value="com.atomikos.icatch.jta.hibernate4"/>                                
  <property name="hibernateProperties">
    <props>                                                
      <prop key="dialect">org.hibernate.dialect.H2Dialect</prop>
      <prop key="current_session_context_class">jta</prop>
      <prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
      <prop key="hibernate.transaction.jta.platform">com.atomikos.icatch.jta.hibernate4.AtomikosPlatform</prop>
      <prop key="hbm2ddl.auto">create</prop>
      <prop key="connection.release_mode">auto</prop>
      <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
      <prop key="show_sql">true</prop>                                                                                        
    </props>
  </property>                
</bean>
manish
  • 19,695
  • 5
  • 67
  • 91
rdm
  • 330
  • 1
  • 4
  • 18
  • Have you checked [the official sample](https://www.atomikos.com/Documentation/HibernateIntegration#Integrating_TransactionsEssentials_4.x_and_Hibernate)? – manish Mar 30 '16 at 03:42
  • Examples use the approach with JPA or Hibernate 3. – rdm Mar 30 '16 at 03:54
  • Examples(after register: examples-hibernate4, examples-hibernate4-jndi, examples-hibernate-jndi-spring) use the approach with JPA or Hibernate 3(on atomikos.com). – rdm Mar 30 '16 at 03:59
  • Did you try `examples-hibernate4-jndi`? It does not use JPA. All test cases included with that example pass. Ignore the `JNDI` part of the example name. It does not necessarily mean that you need a container to run the example. – manish Mar 31 '16 at 07:11
  • I picked up sessionFactory settings of this example, the same error (look at update section) – rdm Mar 31 '16 at 20:51

0 Answers0