Spring+hibernate Multi-Threaded next session problem

Source: Internet
Author: User
Tags sessions try catch

A recent project, with the spring MVC and hibernate3.1 Data Acquisition task function, always executes several times after the page stays in the data refresh state, the query can not be carried out, in-depth understanding of the DAO layer in the program is the Save and update method out of the problem,

@Transactional
public void Save (E entity) {
This.getsession (). Save (entity); The original wording of the project.
This.gethibernatetemplate (). Save (entity);
}

The original wording this.getsession (); In multi-threaded situations, sessions are not automatically freed, successive multiple getsession () operations, spring cannot automatically close these sessions, and no task error prompts even for save to try catch.

This.gethibernatetemplate (). Save the actual use should be This.gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (). Save () method

This.gethibernatetemplate (). Getsessionfactory (). Getcurrentsession ()

Creates a session of a bound thread from the sessionfactory in spring management. Spring automatically determines whether to close the session or postpone it, based on the execution of the thread. Without manual management practices, one thread turns on and off one session and the performance should be the best.

If the use of Hibernatedaosupport provided by the GetSession () method, under normal circumstances is not manual management, but multithreading, you need to perform the session after the completion of the Releasesession (session) method, This method is not recommended for programs that contain multithreading.

If you need to get a session, it is recommended that you use This.gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (); Sessionfactory is thread-safe.


Add: In the case of multithreaded execution

Use: This.gethibernatetemplate (). Getsessionfactory (). Getcurrentsession () Get the session times wrong,
Org.hibernate.HibernateException:No hibernate session bound to thread, and configuration does not allow creation of NON-T Ransactional one here

You need to use the GetSession () method provided by Hibernatedaosupport and manually at the end of the method Releasesession (session).

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.