About Spring3 no Session found for current thread! Configuration and management of and transaction (RPM)

Source: Internet
Author: User

Today I was particularly depressed, originally the project to achieve half, before all good, the results of the afternoon, the problem, bitter force until now to solve. When it comes to a problem, it doesn't hang,
, (days, now found csdn when the QQ expression to the whole come over) in the registered user, gee, backstage found I SQL statement special What is wrong, carefully a look at the data at all did not send to the database, just a simple implementation of the query operation, then I was shocked. The first thing is to see if the action does not write the Save method, and the result is no error. Later I went to another business layer whether there are problems, the results can be normal operation, let me laugh and cry. Later can only check configuration. I saw the two configurations appear at the same time in the project, God, I wonder how the project used to work.

<prop key= "Hibernate.current_session_context_class" >thread</prop>



<prop key= "Hibernate.current_session_context_class" > Org.springframework.orm.hibernate4.springsessioncontext</prop>


These two configurations are redundant in the latest spring3.1.1 and Hibernate 4.1.3,  
that's because in spring transaction management, The current session is bound to Springsessioncontext instead of   in Threadlocalsessioncontext;
And my transactional features are also configured in spring, and Hibernate is managed by spring. Spring is a great housekeeper,  
See this article (very detailed): http://blog.csdn.net/irelandken/article/details/7193123

<!--configuring transaction management--><bean id= "TransactionManager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "><property name=" sessionfactory "ref=" Sessionfactory "/></bean><!--Configuring transaction characteristics--><tx:advice id=" Txadvice "transaction-manager=" TransactionManager "><tx:attributes> <tx:method name=" exists "read-only=" true "/> <tx:method name=" save* "propagation=" REQUIRED "/> <tx:method name=" add* "propagation=" REQUIRED "/> <tx:metho           D name= "create*" propagation= "REQUIRED"/> <tx:method name= "insert*" propagation= "REQUIRED"/> <tx:method name= "update*" propagation= "REQUIRED"/> <tx:method name= "merge*" propagation= "REQUIRED"/ > <tx:method name= "del*" propagation= "REQUIRED"/> <tx:method name= "remove*" propagation= " REQUIRED "/> <tx:method name=" put* "propagation=" REQUIRED "/> <tx:method name=" use* "Propa Gation= "REQUIRED"/> <!--hibernate4 must be configured to turn on the transaction otherwise getcurrentsession () not get the <tx:method name= "           get* "propagation=" REQUIRED "/> <tx:method name=" count* "propagation=" REQUIRED "read-only=" true "/> <tx:method name= "find*" propagation= "REQUIRED" read-only= "true"/> <tx:method name= "list*" Propa gation= "REQUIRED" read-only= "true"/> <tx:method name= "*" propagation= "REQUIRED"/> </tx:attributes& gt;</tx:advice><!--Configure which classes of methods do transaction management--><aop:config proxy-target-class= "true" ><aop:pointcut id= "Bussinessservice" expression= "Execution (* com.shop.service. Impl.*.* (..)) " /><aop:advisor pointcut-ref= "Bussinessservice" advice-ref= "Txadvice"/></aop:config><aop:config Proxy-target-class= "true" ><aop:pointcut id= "DAO" expression= "Execution (* com.shop.dao.*.* (..))"/><aop : Advisor pointcut-ref= "DAO" advice-ref= "Txadvice"/></aop:config>


in many of the above experiments, there are strange and many anomalies:
org.hibernate.HibernateException:No Session found for current thread
Because I have always been the role of Getcurrentsession in the project, there is no transaction management in the business method, the cause of this error is that when the use of getcurrentsession is not found the corresponding transaction, so no session is out, Remember, not because

<prop key= "Hibernate.current_session_context_class" >thread</prop>


This configuration, so be sure to configure the transaction management.
in the back there was a similar:
Org.hibernate.HibernateException:save is not valid without active Transacti
as long as the above problem is solved, the problem is solved,
Summary: Use the latest spring and hibernate to remember to kill the above two configurations and configure the correct corresponding transaction.

About Spring3 no Session found for current thread! Configuration and management of and transaction (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.