No Session found for current thread in spring3! And Transactio

Source: Internet
Author: User

Today, I was very depressed. I had already achieved half of the project, and it was always good before. As a result, I encountered a problem in the afternoon and had to solve it until now. When it encountered a problem, it was not difficult. (today, we only found out when CSDN integrated the QQ expression, I was shocked when I found that something special about SQL statements was wrong. I did not go to the database after reading the data carefully, but simply executed the query operation. First, I checked whether Action has not written the save method, and the result is no error. Later, I went to the other business layer to see if there was a problem, and the results could work properly, making me laugh and cry. Later, you can only check the configuration. At that time, I saw the two configurations appear in the project at the same time. I was amazed at how the project was run before.

[Html]
<SPAN style = "FONT-SIZE: 14px"> <prop key = "hibernate. current_session_context_class"> thread </prop> </SPAN>

<Prop key = "hibernate. current_session_context_class"> thread </prop>
 

[Html]
<SPAN style = "FONT-SIZE: 14px"> <prop key = "hibernate. current_session_context_class"> org. springframework. orm. hibernate4.SpringSessionContext </prop> </SPAN>

<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,

This is because in Spring transaction management, the current Session is bound to SpringSessionContext, rather than in ThreadLocalSessionContext.

My transaction features are also configured in spring, and hibernate is also managed by spring. Spring is really a big manager,

[Html]
<SPAN style = "FONT-SIZE: 14px"> <! -- Configure Transaction Management -->
<Bean id = "transactionManager" class = "org. springframework. orm. hibernate4.HibernateTransactionManager">
<Property name = "sessionFactory" ref = "sessionFactory"/>
</Bean>

<! -- Configure Transaction Features -->
<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: method 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 *" propagation = "REQUIRED"/>
<! -- Hibernate4 must be configured to enable the transaction; otherwise, getCurrentSession () cannot be obtained. -->
<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 *" propagation = "REQUIRED" read-only = "true"/>
<Tx: method name = "*" propagation = "REQUIRED"/>

</Tx: attributes>
</Tx: advice>
 

<! -- Configure the methods of which classes to manage transactions -->
<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> </SPAN>

<! -- Configure Transaction Management -->
<Bean id = "transactionManager" class = "org. springframework. orm. hibernate4.HibernateTransactionManager">
<Property name = "sessionFactory" ref = "sessionFactory"/>
</Bean>
 
<! -- Configure Transaction Features -->
<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: method 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 *" propagation = "REQUIRED"/>
<! -- Hibernate4 must be configured to enable the transaction; otherwise, getCurrentSession () cannot be obtained. -->
<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 *" propagation = "REQUIRED" read-only = "true"/>
<Tx: method name = "*" propagation = "REQUIRED"/>

</Tx: attributes>
</Tx: advice>

 
<! -- Configure the methods of which classes to manage transactions -->
<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>
Many exceptions have occurred in many of the above experiments:

Org. hibernate. HibernateException: No Session found for current thread

Because I have always been a getCurrentSession in the project and have not performed Transaction Management in the business method, the cause of the above error is that the corresponding transaction cannot be found when getCurrentSession is used, so No session comes out. Remember, it's not because

[Html]
<SPAN style = "FONT-SIZE: 14px"> <prop key = "hibernate. current_session_context_class"> thread </prop> </SPAN>

<Prop key = "hibernate. current_session_context_class"> thread </prop>. Therefore, you must configure transaction management.

The following code is also displayed:

Org. hibernate. HibernateException: save is not valid without active transacti

As long as the above problem is solved,

Conclusion: use the latest spring and hibernate to remember to eliminate the above two configurations and correctly configure the corresponding Transaction.

It's very late. It's time to wash and sleep.

 

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.