I have an application with running Spring and Hibernate3. The following is the session factory configuration in Spring 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> <tx:advice id="txAdvice" transaction-manager="txManager"> <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> <aop:config> <aop:pointcut id="projectServiceOperation" expression="execution(* com.service.project.IProjectService.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="projectServiceOperation" /> </aop:config>
It works fine in production.
Now for another project, we are switching to Hibernate4. we copied in the same configuration, except for using Hibernate 4 SessionFactory, TransacionManager, etc. from the org.springframework.orm.hibernate4 package. *. However, he began to throw an exception saying "Retention is invalid without an active transaction." After a short search, many people seem to have encountered problems, and several people have suggested not to use
<prop key="hibernate.current_session_context_class ">thread</prop>
property and it worked. It also worked for me. All the little information I could collect from the posts was about context sessions and the thread strategy that interferes with Spring's session management strategy. But no, where could I find any specific answer.
Also, why does this work with Hibernate3 and not with Hibernate4. What is the difference and what has changed? All other configurations are the same. I do not use @Transactional, but the old-school XML method.
Can someone point me to a clear explanation of this behavioral difference between Hibernate3 and Hibernate4?
source share