Atomikos,Hibernate 4/5,Spring 4,Jetty - 无法找到当前的JTA事务

时间:2016-03-29 22:43:15

标签: spring hibernate jta atomikos

我尝试了很多不同的解决方案,但获得了例外:

  

org.hibernate.HibernateException:无法找到当前的JTA事务

我正在使用Atomikos,Hibernate 4/5,Spring 4和Jetty。

如何配置Spring 4 + Hibernate 4/5(非JPA )?

Atomikos文档只有JPA的例子。

我非常感谢一个有效的例子(非JPA,非JEE应用服务器)。

此示例更改非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>

更新

<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>

0 个答案:

没有答案