简体   繁体   English

Spring 4 + JPA(Hibernate 4)+ JTA事务管理器不会自动刷新

[英]Spring 4 + JPA (Hibernate 4) + JTA transaction manager doesn't flush automatically

I am migrating an application from 我正在从中迁移应用程序

Spring 3.0.5 + JPA 2.0 to Spring 4 + JPA (Hibernate 4) Spring 3.0.5 + JPA 2.0到Spring 4 + JPA(Hibernate 4)

I have followed the migration guide : https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framework . 我遵循了迁移指南: https : //github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framework

The application is using a JTA transaction manager : a Jencks / GeronimoPlatformTransactionManager (because of transactions distributed on datasources and ESB). 该应用程序使用的是JTA事务管理器:Jencks / GeronimoPlatformTransactionManager(由于事务分布在数据源和ESB上)。

The Spring / JPA configuration is : Spring / JPA配置为:

<bean id="rduEntityManagerFactory" class="ch.vd.dsas.rdu.repository.crud.service.ExtendedLocalContainerEntityManagerFactoryBean"
    depends-on="rduTransactionManagerLocator,jGroupsCacheManagerPeerProviderFactoryLocator">
    <property name="persistenceUnitName" value="rduPersistenceUnit" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="${rdu.jpa.database}" />
        </bean>
    </property>
    <property name="persistenceUnitPostProcessors">
        <bean class="ch.vd.dsas.rdu.commons.tx.spring.JtaPersistenceUnitPostProcessor">
            <property name="jtaDataSource" ref="rduDataSource" />
        </bean>
    </property>
    <property name="jpaProperties" ref="jpaProperties"/>
</bean>

<util:properties id="jpaProperties">
    <prop key="javax.persistence.transactionType">JTA</prop>
    <prop key="javax.persistence.validation.mode">CALLBACK</prop>
    <prop key="hibernate.hbm2ddl.auto">${rdu.jpa.hbm2ddl.auto}</prop>
    <prop key="hibernate.current_session_context_class">jta</prop>
    <!-- Transaction properties -->
    <prop key="hibernate.transaction.jta.platform">ch.vd.dsas.rdu.ref.transaction.jencks.JencksTransactionManagerLookup</prop>
    <prop key="hibernate.transaction.manager_lookup_class">ch.vd.dsas.rdu.transaction.jencks.JencksTransactionManagerLookup</prop>
    <prop key="hibernate.default_schema">${rdu.datasource.schemaMetier}</prop>
    <!-- Debug properties -->
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.show_sql">true</prop>
    <!-- Cache properties -->
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.ReplicatedSingletonEhCacheRegionFactory</prop>
    <prop key="hibernate.cache.cluster_name">${rdu.hibernate.cache.jgroups.cluster.name}</prop>
    <prop key="net.sf.ehcache.configurationResourceName">/hibernate-ehcache.xml</prop>
</util:properties>

Transactions are annotation driven : 事务是注释驱动的:

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

The transaction manager is declared like that : 事务管理器的声明如下:

<!-- From Jencks org.jencks:jencks:2.2 -->
<bean id="rduJencksTransactionManager" class="org.jencks.factory.TransactionManagerFactoryBean" />

<bean id="rduJtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <qualifier value="rdu" />
    <property name="transactionManager" ref="rduJencksTransactionManager" />
    <property name="userTransaction" ref="rduJencksTransactionManager" />
</bean>

<bean id="rduTransactionManagerLocator" class="ch.vd.dsas.rdu.transaction.jencks.TransactionManagerLocator" factory-method="getInstance">
    <property name="transactionManager" ref="rduJencksTransactionManager"/>
</bean>

The application is starting and accessing data and displaying it. 该应用程序正在启动并访问数据并显示它。

However no insert / update are performed. 但是,不执行插入/更新。

If I change data and submit the change the application receives the change but the data does not get flushed to the database. 如果我更改数据并提交更改,则应用程序会收到更改,但数据不会刷新到数据库。

I have activated logs and I see the transaction : 我已经激活了日志,并且看到了交易:

rdu 2015-06-18 20:28:01,817 [ http-8080-1] DEBUG [ ostjJtaTransactionManager] Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; rdu 2015-06-18 20:28:01,817 [http-8080-1]调试[ostjJtaTransactionManager]创建名称为[org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]的新事务:PROPAGATION_REQUIRED,ISOLATION_DEFAULT; 'rdu' rdu 2015-06-18 20:28:01,817 [ http-8080-1] DEBUG [ ostjJtaTransactionManager] Participating in existing transaction rdu 2015-06-18 20:28:01,823 [ http-8080-1] DEBUG [ ostjJtaTransactionManager] Initiating transaction commit 'rdu'rdu 2015-06-18 20:28:01,817 [http-8080-1]调试[ostjJtaTransactionManager]参与现有交易rdu 2015-06-18 20:28:01,823 [http-8080-1] DEBUG [ostjJtaTransactionManager ]启动事务提交

But nothing is sent to the database. 但是没有任何内容发送到数据库。

If I intercept the execution through debugging and manually flush the Hibernate session, the data gets updated. 如果我通过调试拦截执行并手动刷新Hibernate会话,则数据将更新。

It seems the JPA / Hibernate session is not coordinated with the transaction. 似乎JPA / Hibernate会话与事务不协调。

I can't figure out what is missing in the configuration and why the session is not flushed automatically. 我不知道配置中缺少什么,以及为什么会话不会自动刷新。

Hope you can help me with this issue. 希望您能帮我解决这个问题。

Best regards, Eric 最好的问候,埃里克

The problem is due to this property: 问题是由于此属性:

<prop key="hibernate.transaction.jta.platform">ch.vd.dsas.rdu.ref.transaction.jencks.JencksTransactionManagerLookup</prop>

The hibernate.transaction.jta.platform property is not the same with hibernate.transaction.manager_lookup_class and it should point to an AbstractJtaPlatform implementation: hibernate.transaction.jta.platform属性与hibernate.transaction.manager_lookup_class ,它应该指向AbstractJtaPlatform实现:

<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM