全部在标题里
我对如何使用 sessionFactory.getCurrentSession()
感到困惑。
在我的网络应用程序中,创建多线程来将消息保存在数据库中,并且在创建每个线程时,我从未使用过 openSession()
方法,所有 Daos
使用getCurrentSession()
。到目前为止,一切正常。
并且没有配置CurrentSessionContext
。那么 hibernate 是否会为此线程生成一个新 session ,如果不是的话,这个 getCurrentSession 是如何工作的?
更新
是的,我正在使用 Spring 3.1。
Tomcat6(作为 Eclipse 中的插件和 Linux 计算机上的真实服务器,该应用程序可以在两台服务器上运行)
最佳答案
Spring HibernateTransactionManager将当前 session 绑定(bind)到 Activity 事务,允许 getCurrentSession() 返回与正在进行的 Spring 事务关联的 session :
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.
关于java - Spring Hibernate getCurrentSession() 当没有 session 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24391606/