java - Spring Hibernate getCurrentSession() 当没有 session 时?

标签 java spring hibernate

全部在标题里 我对如何使用 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/

相关文章:

java - 电报机器人 java : inline url button does not create an update and thus no callback data to respond on

java - Spring中Hibernate sessionFactory的配置

java - 如何在java对象的JAXB中将字段名称大写?

java - Freemarker 通过 Java 设置 checkTemplateLocation

java.lang.NoSuchMethodError : org. hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/classic/Session;

java - 使用 mongorepository 更新特定字段

java - Playframework 2.1.1 部署在生产服务器上

java - Java 中的隐式和显式并行

java - spring + hibernate配置错误?类路径资源 [spring.xml] 中的 XML 文档无效

hibernate - 在一个 Hibernate 查询中更新多个列?