简体   繁体   中英

Spring Hibernate getCurrentSession() when there's no session?

All in the title I'm confused with how to use sessionFactory.getCurrentSession() .

In my web-app, multi-threads are created to save messages in the DB, and on creating of each thread, I never used openSession() method, all of the Daos use getCurrentSession() . And till now it's working fine.

And there's no CurrentSessionContext configured. So does hibernate generate a new session for this thread, if not how does this getCurrentSession work ?

UPDATE

Yes I'm using Spring 3.1.

Tomcat6 (as plugin in Eclipse and real server on a Linux machine, the app works on both server)

The Spring HibernateTransactionManager binds a current session to the active transaction, allowing getCurrentSession() to return the session associated to the Spring transaction in progress:

PlatformTransactionManager implementation for a single Hibernate SessionFactory. Binds a Hibernate Session from the specified factory to the thread, potentially allowing for one thread-bound Session per factory. SessionFactory.getCurrentSession() is required for Hibernate access code that needs to support this transaction handling mechanism, with the SessionFactory being configured with SpringSessionContext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM