My question is about hibernate session scope in Grails :
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.