简体   繁体   中英

Hibernate session scope in Grails

My question is about hibernate session scope in Grails :

  1. Is session bound to a spring transaction?
  2. A new session is created whenever a new request is created?
  3. Or session is bound to a thread?

Can this be explained?

Thanks

Sessions are bound to transactions since the active PlatformTransactionManager is a HibernateTransactionManager . But be aware that controller requests do not run in transactions - only services are automatically transactional. You should refactor controllers that write to the database to use services to do that work so it's properly transactional.

A new session is created for each request and bound to the thread by the GrailsOpenSessionInViewInterceptor . This helps to avoid lazy loading exceptions and other issues. The session is flushed and closed at the end of the thread.

The ThreadLocal is in TransactionSynchronizationManager if you're curious; you can call TransactionSynchronizationManager.getResource(sessionFactory) (use def sessionFactory to get access to the SessionFactory ) to access the SessionHolder that holds the session.

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