0

我目前正在从事一个项目,其中包括 EJB 3.0(无状态 SB)、JPA(作为提供者的 Hibernate)、作为事务管理器的 JTA。应用服务器是 JBoss AS 7。Spring 用于集成 EJB 和 JPA。

一切似乎都工作正常,除非在 EJB 中发生任何异常,然后持久性单元被 Spring 关闭。在随后的请求中,将再次创建持久性单元,这会变得很耗时,并且在理想情况下也不应该发生。

下面是详细配置

持久性.xml

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<class>com.test.User</class>
<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>

spring-application-context.xml

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

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

<jee:jndi-lookup id="dataSource" jndi-name="java:/datasources/test" />

<bean id="entityManagerFactory"
 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="generateDdl" value="false" />
    <property name="database" value="MYSQL" />
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
    </bean>
</property>
<property name="jpaPropertyMap">
    <map>
    <entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"></entry>
    <entry key="hibernate.current_session_context_class" value="jta" />
    <entry key="hibernate.connection.release_mode" value="auto" />
   </map>
</property>
<property name="persistenceUnitPostProcessors">
       <list>
        <bean class="com.transaction.processor.JtaPersistenceUnitPostProcessor">
                <property name="jtaMode" value="true"/> 
            <property name="jtaDataSource" ref="dataSource"/>
        </bean>
       </list>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:/TransactionManager"></property>
<property name="autodetectUserTransaction" value="false"></property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

JtaPersistenceUnitPostProcessor 类负责将事务类型设置为 JTA,将数据源设置为 jta-datasource。

任何人都可以请提供任何帮助。

提前致谢。

4

2 回答 2

0
 <bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManagerName" value="java:jboss/TransactionManager" />
    <property name="userTransactionName" value="java:comp/UserTransaction" />
  </bean>

 <tx:annotation-driven transaction-manager="transactionManager" />


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

您没有指定任何错误消息。您可以在配置文件中添加这些行。

于 2014-04-22T08:41:10.353 回答
0

I see you use JTA transaction manager and use that only if you use distributed Transaction and use JNDI. JTA tran. manager listens TX happening through connection acquired from JNDI datasource. If you have datasource created in your code and is not a part of Web container but is limited inside app. container in your web server, JTA wont work.

If you want to implement Tx manager with in a single app. context go for JPA transaction manager which is very reliable.

于 2018-04-20T16:58:02.960 回答