简体   繁体   中英

Spring + Hibernate session lifecycle

How properly "lifecycle" of a Hibernate session under Spring should be done?

The SessionFactory is created automatically by Spring and is taking its DB connections from Glassfish connection pool. At the moment I am getting a Hibernate session via SessionFactory.getCurrentSession() . Then I start transaction, do the work and then commit() or rollback() at the end. 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?

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. Check out 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

I get following lines from this link

The main contract here is the creation of Session instances. Usually an application has a single SessionFactory instance and threads servicing client requests obtain Session instances from this factory. The internal state of a SessionFactory is immutable. 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

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. In case of JDBC transaction is used , it is set to ConnectionReleaseMode.AFTER_TRANSACTION (ie 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()

You can verify this behaviour in hibernate documentation Section 11.5 .

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

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.

When you are executing a hibernate query through SessionFactory.getCurrentSession() , Spring performs the necessary task of opening and closing the connection . The SessionFactory you are using in the spring config also calls the config.buildSessionFactory method internally .

Most of this happens in the implementations of the AbstractSessionFactoryBean. The closing of connecting is done by hibernate in the SessionFactoryImpl class using the statement settings.getConnectionProvider().close(); . In short , hibernate does everything for you . Spring just calls it's help when necessary.

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