The chosen transaction strategy requires access to

2020-06-03 09:15发布

问题:

I am using Spring 2.0.6 and Hibernate 3.2.x on apache tomcat5.5,now we planed to changed our hybernate mapping files into hybernate+jpa support mapping files. for that we created files like this

daoConfig.xml

 <beans:bean id="dataSource" 
  class="org.springframework.jndi.JndiObjectFactoryBean">
      <beans: property name="jndiName">
    <beans: value>java:/comp/jdbc/Paymentsdb</beans:value>  
   </beans: property>
  </beans: bean> 
 <beans: bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <beans: property name="useTransactionAwareDataSource"
   value="true" />
  <beans: property name="dataSource">
   <beans:ref bean="dataSource"/>
  </beans: property>
  <beans: property name="hibernateProperties">
   <beans: props>
    <beans:prop key="hibernate.dialect">${database.target}</beans:prop>
    <beans:prop key="hibernate.connection.isolation">3</beans:prop>
    <beans:prop key="hibernate.current_session_context_class">jta</beans:prop>
    <beans:prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory
    </beans:prop>
    <beans: prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
    </beans: prop>
    <beans: prop key="hibernate.connection.release_mode">on_close</beans: prop>
    <beans: prop key="hibernate.show_sql">false</beans: prop>
   </beans: props>
  </beans: property>
 </beans: bean>

 <beans: bean id="jpaTemplate"
  class="org.springframework.orm.jpa.JpaTemplate">
  <beans: property name="entityManagerFactory">
   <beans:ref bean="entityManagerFactory" />
  </beans: property>
 </beans: bean>

 <beans: bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <beans: property name="dataSource">
    <beans:ref bean="dataSource" />
  </beans: property>    
        <beans: property name="jpaVendorAdapter">
         <beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                        <beans: property name="generateDdl" value="false"/>
                        <beans: property name="showSql" value="true" />
                        <beans: property name="databasePlatform" value="${database.target}" />
                </beans: bean>
        </beans: property>
        <beans: property name="persistenceXmlLocation">
   <beans:value>classpath:META-INF/persistence.xml</beans: value>
  </beans: property>    
    </beans: bean>   

<beans:bean id="atomikosTransactionManager"
    class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
    destroy-method="close">
    <beans:property name="forceShutdown" value="true" />
    <beans:property name="startupTransactionService" value="true" />
</beans:bean>

<beans:bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <beans:property name="transactionTimeout">
        <beans:value>300</beans:value>
    </beans:property>
</beans:bean>

<beans:bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <beans:property name="transactionManager">
        <beans:ref bean="atomikosTransactionManager" />
    </beans:property>
    <beans:property name="userTransaction">
        <beans:ref bean="atomikosUserTransaction" />
    </beans:property>
</beans:bean>

</beans: beans> 

in my daoconfig.xml i given path for persistence.xml and this is my persistence.xml file

  <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
 version="1.0">
 <persistence-unit name="payhub" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
         <mapping-file>META-INF/orm.xml</mapping-file>
   <class>com.evolvus.common.model.TaUser</class>
           ----------
           ----------
           ---------- 
    <class>com.evolvus.common.model.TaBooks</class>
    </persistence-unit>
</persistence>

here i am specifying my orm mapping file name
orm.xml file is

 <?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
 http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
 version="1.0">
 <description>Welcome</description> 
 <package>com.evolvus.common.model</package>

 <!--  TaUser starts-->
 <entity class="com.evolvus.common.model.TaUser" name="TaUser">
  <table name="ta_user" />
  <attributes>
   <id name="userId">
   <column name="USER_ID"/>
    <generated-value strategy="TABLE" />
   </id>
   <basic name="userName">
    <column name="USER_NAME" length="50" />
   </basic>
   <basic name="password">
    <column name="PASS_WORD" length="50" />
   </basic>
  </attributes>
 </entity>
 <entity class="com.evolvus.common.model.TaBooks" name="TaBooks">
  <table name="ta_user"/>
  <attributes>
    --------------
    --------------
    ------------ 
  </attributes>
 </entity>
  </entity-mappings> 

when i am trying to run my server .i Am getting the error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaTemplate' defined in ServletContext resource [/WEB-INF/xaconfig/daoJPAConfig.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/xaconfig/daoJPAConfig.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: payhub] Unable to build EntityManagerFactory
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/xaconfig/daoJPAConfig.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: payhub] Unable to build EntityManagerFactory
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
 Caused by: javax.persistence.PersistenceException: [PersistenceUnit: payhub] Unable to build EntityManagerFactory
 at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
 at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132) 
Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
 at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:329)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)  
 ... 42 more 

give me any suggestions

回答1:

First of all, you're creating both a Hibernate SessionFactory and a JPA EntityManagerFactory. That does not make very much sense unless you have a very odd setup, and you should very likely throw out the SessionFactory configuration. You can either configure plain vanilla Hibernate, or you can configure JPA, you don't need both.

The creation of the entity manager factory fails because you specified JTA as the transaction type, but the JTA transaction manager lookup class is not defined. (You defined it in the Hibernate SessionFactory config, but not in your JPA config.)

Add this to your persistence.xml:

<persistence-unit name="payhub" transaction-type="JTA">

    ...

    <properties>
        <property name="hibernate.transaction.manager_lookup_class" 
            value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
    </properties>               
</persistence-unit>


回答2:

For those who are using Jboss AS, this configuration could be used:

<property name="hibernate.transaction.manager_lookup_class"  
 value="org.hibernate.transaction.JBossTransactionManagerLookup"/>

Solution extracted from: http://www.gregbugaj.com/?p=172