when using hibernate with spring, can someone explain how the session unit of work and transactions are handled?
- is the transaction started at the beginning of the page request, and committed at the end?
- can I have multiple db calls per request, that each have different transaction levels? e.g. some are left as default, while others are read-uncommitted?
In a web application, opening / closing the
Sessionis usually done using the “Open Session in View” pattern. Spring comes with aOpenSessionInViewFilteror anOpenSessionInViewInterceptorfor this. Both make HibernateSessionsavailable via the current thread, which will be autodetected by transaction managers. It is suitable for service layer transactions viaHibernateTransactionManagerorJtaTransactionManageras well as for non-transactional execution (if configured appropriately).Transaction demarcation is usually done at the service methods level, using Spring AOP to wrap them inside transactions.
You can have nested transactions with different isolation levels. Refer to the Transaction Management chapter.