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).