简体   繁体   English

Spring + Hibernate会话生命周期

[英]Spring + Hibernate session lifecycle

How properly "lifecycle" of a Hibernate session under Spring should be done? 应该如何正确地完成Spring下Hibernate会话的“生命周期”?

The SessionFactory is created automatically by Spring and is taking its DB connections from Glassfish connection pool. SessionFactory由Spring自动创建,并从Glassfish连接池获取其数据库连接。 At the moment I am getting a Hibernate session via SessionFactory.getCurrentSession() . 目前我正通过SessionFactory.getCurrentSession()获得Hibernate会话。 Then I start transaction, do the work and then commit() or rollback() at the end. 然后我开始事务,完成工作,然后在最后commit()rollback() Do I need to do any other actions like disconnect() , close() , flush() or any others at any time so connections would be properly returned back to the pool or is everything already automatically done by Spring? 我是否需要随时执行任何其他操作,如disconnect()close()flush()或任何其他操作,以便连接可以正确地返回到池中,或者Spring已经自动完成了所有操作?

With plenty of these methods it is a little confusing for me at the moment to understand when what should be done, maybe someone can point to right direction? 有了很多这些方法,我现在有点困惑,要了解应该做什么,也许有人可以指向正确的方向?

As SessionFactory is created automatically by Spring, Spring framework will take care of closing the connection. 由于SessionFactory是由Spring自动创建的,因此Spring框架将负责关闭连接。 Check out Spring Resource Management 查看Spring Resource Management

If you want to check. 如果你想检查。 You can check the log, if you are using logging for your app. 如果您正在使用应用程序的日志记录,则可以检查日志。 It'll be like : 它会像:

(main) INFO [AnnotationSessionFactoryBean] Closing Hibernate SessionFactory (主要)INFO [AnnotationSessionFactoryBean]关闭Hibernate SessionFactory

I get following lines from this link 我从这个链接获得以下行

The main contract here is the creation of Session instances. 这里的主要合同是创建Session实例。 Usually an application has a single SessionFactory instance and threads servicing client requests obtain Session instances from this factory. 通常,应用程序具有单个SessionFactory实例,服务客户端请求的线程从此工厂获取Session实例。 The internal state of a SessionFactory is immutable. SessionFactory的内部状态是不可变的。 Once it is created this internal state is set. 创建后,将设置此内部状态。 This internal state includes all of the metadata about Object/Relational Mapping. 此内部状态包括有关对象/关系映射的所有元数据。 Implementors must be threadsafe. 实现者必须是线程安全的。

The policies about how the connection releases back to the connection pool have nothing to do with Spring .It is configured by Hibernate itself through the configuration parameter hibernate.connection.release_mode , which is identified by the enum in the org.hibernate.ConnectionReleaseMode 关于连接如何释放回连接池的策略与Spring无关。它由Hibernate本身通过配置参数hibernate.connection.release_mode配置,该参数由org.hibernate.ConnectionReleaseMode中的枚举标识。

Start from version 3.1+ , the default value of the hibernate.connection.release_mode is auto which the corresponding ConnectionReleaseMode value depends on whether JTA or JDBC transaction is used. 从版本3.1+开始, hibernate.connection.release_mode的默认值为auto ,相应的ConnectionReleaseMode值取决于是使用JTA还是JDBC事务。 In case of JDBC transaction is used , it is set to ConnectionReleaseMode.AFTER_TRANSACTION (ie after_transaction ). 如果使用JDBC事务,则将其设置为ConnectionReleaseMode.AFTER_TRANSACTION (即after_transaction )。

The behaviour of ConnectionReleaseMode.AFTER_TRANSACTION is that : The connection will be returned to the connection pool after each transaction , that is by calling either transaction.commit() or transaction.rollback() , as well as calling session.close() and session.disconnect() ConnectionReleaseMode.AFTER_TRANSACTION的行为是:在每次事务之后,连接将返回到连接池,即通过调用transaction.commit()transaction.rollback() ,以及调用session.close()session.disconnect()

You can verify this behaviour in hibernate documentation Section 11.5 . 您可以在第11.5节的hibernate文档中验证此行为。

Hope this link will guide you about session and transactions. 希望链接将指导您有关会话和交易的信息。

Then I start transaction, do the work and then commit() or rollback() at the end. 然后我开始事务,完成工作,然后在最后提交()或rollback()。 Do I need to do any other actions like disconnect(), close(), flush() or any others at any time so connections would be properly returned back to the pool or is everything already automatically done by Spring? 我是否需要随时执行任何其他操作,如disconnect(),close(),flush()或任何其他操作,以便连接可以正确地返回到池中,或者Spring已经自动完成了所有操作?

As you call commit() on Transaction it will automatically closes the session, which ultimately calls close method on connection to return to it's pool. 当您在Transaction上调用commit()时,它将自动关闭会话,最终会在连接上调用close方法返回到它的池。

When you are executing a hibernate query through SessionFactory.getCurrentSession() , Spring performs the necessary task of opening and closing the connection . 当您通过SessionFactory.getCurrentSession()执行hibernate查询时,Spring执行打开和关闭连接的必要任务。 The SessionFactory you are using in the spring config also calls the config.buildSessionFactory method internally . 您在spring配置中使用的SessionFactory也在内部调用config.buildSessionFactory方法。

Most of this happens in the implementations of the AbstractSessionFactoryBean. 大多数情况发生在AbstractSessionFactoryBean的实现中。 The closing of connecting is done by hibernate in the SessionFactoryImpl class using the statement settings.getConnectionProvider().close(); 连接的关闭是通过SessionFactoryImpl类中的hibernate使用语句settings.getConnectionProvider()。close();来完成的。 . In short , hibernate does everything for you . 简而言之,hibernate为您做了一切。 Spring just calls it's help when necessary. Spring会在必要时调用它的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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