views:

22

answers:

1

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
so a session per transaction?
stinghu
@stinghu Yes, one Session per transaction/thread.
Pascal Thivent
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
@stinghu Yes, that's right.
Pascal Thivent
@stinghu provided that the controller method is not marked `@Transactional` itself, that is.
seanizer