I have an application with Spring and Hibernate3 running well in production. Following is the config for session factory in Spring's applicationContext.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/hibernate</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.autocommit">false</prop>
<prop key="hibernate.current_session_context_class ">thread</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean
below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="count*" propagation="SUPPORTS" read-only="true" />
<tx:method name="validate*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="login" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any execution of
an operation defined by the service interfaces -->
<aop:config>
<aop:pointcut id="projectServiceOperation"
expression="execution(* com.service.project.IProjectService.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="projectServiceOperation" />
</aop:config>
It is working fine in production.
Now for another project we are migrating to Hibernate4. we copied over the same configuration except using Hibernate 4's SessionFactory,TransacionManager etc. from org.springframework.orm.hibernate4.* package. However it started giving exception saying "Save is not valid without active transaction". After searching a bit many people seemed to have faced the problems and several people suggested not to use
<prop key="hibernate.current_session_context_class ">thread</prop>
property and it worked. It also worked for me. All little information I could gather from the posts that it has something to do with contextual sessions and thread strategy interfering with Spring's session management strategy. But no where I could find any concrete answer.
Also, why did it work with Hibernate3 and not with Hibernate4. What is the difference and what has changed? Every other configuration is same. I am not using @Transactional but the old school XML way.
Can somebody point me to a clear explanation for this behavioral difference between Hibernate3 and Hibernate4?