<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=xarx" rel="nofollow">Xarx</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8344?redirect=false" rel="nofollow">SPR-8344</a></strong> and commented</p>
<p dir="auto">What happens:<br>
When org.springframework.beans.factory.support.DefaultListBeanFactory.findAutowireCandidates() determines candidate beans, it instantiates all of them at the end. Even when the candidates are marked as lazy-inited. In my opinion, this is unnecessary. Lazy-inited candidate should be instantiated only when it is actually chosen to be the right candidate - which is done later in DefaultListBeanFactory.doResolveDependency().</p>
<p dir="auto">What should happen:<br>
In my opinion, beans marked as lazy-init should be instantiated only when actually used by the application code. Spring internals should avoid instantiating them unless really necessary.</p>
<p dir="auto">This bug is a generalization of Bug <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398112340" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12991" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12991/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12991">#12991</a>. I filed this bug separately because its resolution might be more complicated (though it needn't) than <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398112340" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12991" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12991/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12991">#12991</a>, hence this bug has less chance to be resolved quickly.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0.5</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="398107689" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12240" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12240/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12240">#12240</a> Migration from 2.5.6 to 3.0.2 - Lazy Init functions differently. (<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="398112340" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12991" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12991/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12991">#12991</a> Even when primary candidate exists, other lazy bean candidates are instantiated (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>
<p dir="auto">4 votes, 5 watchers</p>
<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>