简体   繁体   中英

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)

I have followed the migration guide : 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).

The Spring / JPA configuration is :

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

But nothing is sent to the database.

If I intercept the execution through debugging and manually flush the Hibernate session, the data gets updated.

It seems the JPA / Hibernate session is not coordinated with the transaction.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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