<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=alysson.rodrigues" rel="nofollow">Alysson Rodrigues</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9424?redirect=false" rel="nofollow">SPR-9424</a></strong> and commented</p>
<p dir="auto">Hibernate 4.1.0.Final isn´t auto flushing during before completion phase when using JTA (CMT) under Spring 3.1.1.RELEASE and JBoss AS 7.1.</p>
<p dir="auto">I´ve set hibernate.transaction.factory_class to org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory and hibernate.transaction.jta.platform to org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform. My transaction manager bean is a org.springframework.transaction.jta.JtaTransactionManager configured bean.</p>
<p dir="auto">Then, logging (TRACE mode) shows me the following info:</p>
<p dir="auto">15:23:58,555 TRACE [org.springframework.transaction.interceptor.TransactionInterceptor] (http--127.0.0.1-8080-1) Completing transaction for...</p>
<p dir="auto">15:23:58,557 TRACE [org.springframework.transaction.jta.JtaTransactionManager] (http--127.0.0.1-8080-1) Triggering beforeCommit synchronization<br>
15:23:58,558 TRACE [org.springframework.transaction.jta.JtaTransactionManager] (http--127.0.0.1-8080-1) Triggering beforeCompletion synchronization<br>
15:23:58,559 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] (http--127.0.0.1-8080-1) Closing JPA EntityManager<br>
15:23:58,560 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] (http--127.0.0.1-8080-1) Initiating transaction commit<br>
15:23:58,561 TRACE [org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization] (http--127.0.0.1-8080-1) JTA sync : beforeCompletion()<br>
15:23:58,562 TRACE [org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl] (http--127.0.0.1-8080-1) Transaction before completion callback<br>
15:23:58,564 TRACE [org.hibernate.internal.SessionImpl] (http--127.0.0.1-8080-1) before transaction completion<br>
15:23:58,566 TRACE [org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization] (http--127.0.0.1-8080-1) JTA sync : afterCompletion(3)<br>
15:23:58,567 TRACE [org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl] (http--127.0.0.1-8080-1) Transaction after completion callback [status=3]<br>
15:23:58,568 TRACE [org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl] (http--127.0.0.1-8080-1) after transaction completion<br>
15:23:58,569 DEBUG [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http--127.0.0.1-8080-1) Aggressively releasing JDBC connection<br>
15:23:58,570 TRACE [org.hibernate.internal.SessionImpl] (http--127.0.0.1-8080-1) after transaction completion<br>
15:23:58,571 DEBUG [org.hibernate.ejb.EntityManagerImpl] (http--127.0.0.1-8080-1) Closing entity manager after transaction completion<br>
15:23:58,572 TRACE [org.hibernate.internal.SessionImpl] (http--127.0.0.1-8080-1) Closing session<br>
15:23:58,573 TRACE [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http--127.0.0.1-8080-1) Closing logical connection<br>
15:23:58,574 TRACE [org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler] (http--127.0.0.1-8080-1) Handling invocation of connection method [close]<br>
15:23:58,575 TRACE [org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler] (http--127.0.0.1-8080-1) Invalidating connection handle<br>
15:23:58,576 TRACE [org.hibernate.engine.jdbc.internal.JdbcResourceRegistryImpl] (http--127.0.0.1-8080-1) Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcResourceRegistryImpl@17072bf]<br>
...</p>
<p dir="auto">HOWEVER, IF I use org.springframework.orm.jpa.JpaTransactionManager, instead of JtaTransactionManager, adjusting the configurations above, logging shows me:</p>
<p dir="auto">16:40:36,014 TRACE [org.springframework.transaction.interceptor.TransactionInterceptor] (http--127.0.0.1-8080-1) Completing transaction for...</p>
<p dir="auto">16:40:36,015 TRACE [org.springframework.orm.jpa.JpaTransactionManager] (http--127.0.0.1-8080-1) Triggering beforeCommit synchronization<br>
16:40:36,015 TRACE [org.springframework.orm.jpa.JpaTransactionManager] (http--127.0.0.1-8080-1) Triggering beforeCompletion synchronization<br>
16:40:36,016 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] (http--127.0.0.1-8080-1) Initiating transaction commit<br>
16:40:36,016 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] (http--127.0.0.1-8080-1) Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@2d885a]<br>
16:40:36,017 DEBUG [org.hibernate.engine.transaction.spi.AbstractTransactionImpl] (http--127.0.0.1-8080-1) committing<br>
16:40:36,017 TRACE [org.hibernate.internal.SessionImpl] (http--127.0.0.1-8080-1) Automatically flushing session<br>
16:40:36,017 TRACE [org.hibernate.event.internal.AbstractFlushingEventListener] (http--127.0.0.1-8080-1) Flushing session<br>
16:40:36,018 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener] (http--127.0.0.1-8080-1) Processing flush-time cascades<br>
16:40:36,018 TRACE [org.hibernate.engine.internal.Cascade] (http--127.0.0.1-8080-1) Processing cascade ACTION_PERSIST_ON_FLUSH for: br.gov.tcu.sgos.modelo.entidade.ordemservico.OrdemServico<br>
16:40:36,019 TRACE [org.hibernate.engine.spi.CascadingAction] (http--127.0.0.1-8080-1) Cascading to persist on flush: br.gov.tcu.sgos.modelo.entidade.contrato.Contrato<br>
16:40:36,020 TRACE [org.hibernate.engine.internal.Cascade] (http--127.0.0.1-8080-1) Done processing cascade ACTION_PERSIST_ON_FLUSH for: br.gov.tcu.sgos.modelo.entidade.ordemservico.OrdemServico<br>
16:40:36,021 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener] (http--127.0.0.1-8080-1) Dirty checking collections<br>
16:40:36,021 TRACE [org.hibernate.event.internal.AbstractFlushingEventListener] (http--127.0.0.1-8080-1) Flushing entities and processing referenced collections<br>
16:40:36,022 TRACE [org.hibernate.persister.entity.AbstractEntityPersister] (http--127.0.0.1-8080-1) br.gov.tcu.sgos.modelo.entidade.ordemservico.OrdemServico.descricao is dirty<br>
16:40:36,022 TRACE [org.hibernate.event.internal.DefaultFlushEntityEventListener] (http--127.0.0.1-8080-1) Found dirty properties [[br.gov.tcu.sgos.modelo.entidade.ordemservico.OrdemServico#5]] : [Ljava.lang.String;<code class="notranslate">@1882506</code><br>
16:40:36,023 TRACE [org.hibernate.event.internal.DefaultFlushEntityEventListener] (http--127.0.0.1-8080-1) Updating entity: [br.gov.tcu.sgos.modelo.entidade.ordemservico.OrdemServico#5]<br>
16:40:36,024 TRACE [org.hibernate.engine.internal.Versioning] (http--127.0.0.1-8080-1) Incrementing: 2 to 3<br>
16:40:36,026 TRACE [org.hibernate.event.internal.AbstractFlushingEventListener] (http--127.0.0.1-8080-1) Processing unreferenced collections<br>
16:40:36,026 TRACE [org.hibernate.event.internal.AbstractFlushingEventListener] (http--127.0.0.1-8080-1) Scheduling collection removes/(re)creates/updates<br>
16:40:36,027 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener] (http--127.0.0.1-8080-1) Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects</p>
<p dir="auto">SO, is something missing? Some additional configuration? Using JpaTransactionaManager everything works as expected, but when using JtaTransactionManager there is no auto flush during the before completion phase.</p>
<p dir="auto">Could you please help me?</p>
<p dir="auto">I´m looking forward to hearing good news from you.</p>
<p dir="auto">Thank you.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1.1</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398150499" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14040" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14040/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14040">#14040</a> Session not flushed with Hibernate 4.1.3, JTA and Spring transaction management integration (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=koen.serneels" rel="nofollow">Koen Serneels</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9404?redirect=false" rel="nofollow">SPR-9404</a></strong> and commented</p>
<p dir="auto">I have been migrating from hibernate 3.3.x to hibernate 4.1.3-final using Spring 3.1.1-release.<br>
Besides the hibernate specific refactorings (due to API changes) I thought the only changes for the Spring integration were changing packages names:</p>
<ul dir="auto">
<li>Use the LocalSessionFactoryBean from the hibernate4 package</li>
<li>Use the HibernateTransactionManger (which we only use for testing) from the hibernate4 package.</li>
</ul>
<p dir="auto">As it turned out the migration when smooth and everything was working on a local tomcat.<br>
However, once we ran our app on glassfish with the JtaTransactionManager (I tested it on GF3.1.2) we got a "No Session found for current thread" when obtaining sessionFactory.currentSession().<br>
After checking SpringSessionContext, we learned that if TransactionSynchronizationManager does not return a existing session or SessionHolder, a check<br>
is performed for a jtaSessionContext, which was null.<br>
The fact that no SessionHolder is registered made also sense as this done by the HibernateTransactionManager and we are using the JtaTransactionManager.</p>
<p dir="auto">So then learned that in case of JTA you have to specify manually how the tx manager/user transaction can be found.<br>
This was done automatically for you in the hibernate3 LocalSessionFactoryBean, but no longer in the hibernate4 LocalSessionFactoryBean.<br>
So to solve this we configured:</p>
<p dir="auto">hibernate.transaction.jta.platform and set it to SunOneJtaPlatform.</p>
<p dir="auto">This resolved the "No Session found for current thread" as it initialized the jtaSessionContext with the txmanager provided by the configured JtaPlatform.</p>
<p dir="auto">However, now it turns out that the hibernate session is not flushed before the transaction is commited and hence no modifications are written to database.<br>
In the supplied sample we have a basic hibernate setup with H2. Next we have a JtaTransactionManager and a transactional facade.<br>
Next we have a test entity having a single property. The facade has two methods, one to store the entity and one to retrieve the entity.<br>
They both marked as <code class="notranslate">@Transactional</code> and if called will run in there own transaction.<br>
The trigger is a plain JEE servlet which retrieves the facade from application context.<br>
First the store method is called (tx1) then the retrieve method is called (tx2).<br>
As you will see with the Spring hibernate4 integration there was nothing saved.<br>
With the hibernate3 integration everything works as expected and the record is saved<br>
(it can be retrieved by the subsequent retrieval transaction)</p>
<p dir="auto">What is also bizarre is that in hibernate3 modus everything goes via the Spring TransactionSynchronizationManager (even in JTA mode).<br>
Also the current session is bound via a thread local referenced via the synchronization.<br>
This is bound using a SpringSessionSynchronization which will call flush before transaction completion.</p>
<p dir="auto">All of this is gone with the hibernate4 integration from the moment a JTA environment is detected.<br>
As of then everything goes via the JTA transaction manager, as there where no Spring Transacion management involved.<br>
This could be normal to a certain extend, but it feels odd compared to the way is was done with hibernate3.</p>
<p dir="auto">I supplied two samples:</p>
<ul dir="auto">
<li>
<p dir="auto">hibernate3.zip : this is the working one, deploy it on GF and goto "<a href="http://localhost:8080/hibernate3/Persist" rel="nofollow">http://localhost:8080/hibernate3/Persist</a>"<br>
You will see that it stores a record and is able to retrieve it again</p>
</li>
<li>
<p dir="auto">hibernate4.zip : the exact same sample as above, but now with hibernate4 and using LocalSessionFactoryBean from the hibernate4 package and the hibernate.transaction.jta.platform set.<br>
You will see that it stores a record and is NOT able to retrieve it.</p>
</li>
</ul>
<p dir="auto">Both samples have a POM so it should be trivial to build them.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1.1</p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/19646/hibernate3.zip" rel="nofollow">hibernate3.zip</a> (<em>7.33 kB</em>)</li>
<li><a href="https://jira.spring.io/secure/attachment/19647/hibernate4.zip" rel="nofollow">hibernate4.zip</a> (<em>7.35 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398151014" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14115" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14115/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14115">#14115</a> Hibernate 4 smart flushing does not work unless CMTTransactionFactory is being specified (<em><strong>"duplicates"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398109875" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12571" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12571/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12571">#12571</a> util:map support for key-types other than String not working (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398150642" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14060" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14060/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14060">#14060</a> Spring JtaTransactionManager is not flushing during before completion phase when using Hibernate 4 or Hibernate 3 (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>
<p dir="auto">5 votes, 4 watchers</p>