I have a 3 layer application using spring and hibernate (controller -> service -> dao) and transaction is applied to service layer. I don't configure OpenSessionInViewInterceptor or OpenSessionInViewFilter and I want to know the hibernate session control behavior. Open session per transaction or per request? Thanks!
+2
A:
If you're using the HibernateTransactionManager
, a Session
will get bound to the current thread and flushed and closed when the transaction ends, either through commit or roll back.
See also
Pascal Thivent
2010-10-13 06:47:50
so a session per transaction?
stinghu
2010-10-13 06:54:29
@stinghu Yes, one Session per transaction/thread.
Pascal Thivent
2010-10-13 07:03:30
Thanks, Pascal! Another question, if one controller calls two different service method (they are in different transactions but in only one thread), there should be two sessions opened. Am I right?
stinghu
2010-10-13 07:26:13
@stinghu Yes, that's right.
Pascal Thivent
2010-10-13 07:38:21
@stinghu provided that the controller method is not marked `@Transactional` itself, that is.
seanizer
2010-10-13 09:01:40