Using current_session_context_class property hibernate 3 hibernate 4

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> <!-- 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 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?

+9
source share
1 answer

It depends on the version of spring, but there is generally a mess with this property, while using spring should be avoided (if you are not using JTA for transactions, then you need to configure this).

Hibernate 3.1 has something called contextual sessions, and the CurrentSessionContext interface is provided for this sleep mode. There are several implementations for this (and thread is short for ThreadLocalSessionContext ). spring has its own implementation of this SpringSessionContext class interface.

Spring sets the Springs implementation property CurrentSessionContext , when it is set to something else (other than JTA), it will break the springs that allow you to control the hibernate session (and therefore the transaction).

Now in older versions of spring (and I assume that you also updated spring to be able to use hibernate) in conjunction with hibernate 3, some other trickery involved in getting a session (due to backward compatibility with old 3 .x version of hibernate). This made spring less dependent on the value of this property. (Spring created a proxy for SessionFactory and basically intercepted the getSession method to be able to control the session).

+9
source

Source: https://habr.com/ru/post/957733/


All Articles