Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Session in View vs @Transactional

I had been using @Transactional annotations in my Service Layer. But to resolve an error due to Lazy Loading in View I had to use Open Session in View Filter. After this, without use of @Transaction itself a Session gets opened and transaction starts. So does that mean @Transactions are not required? How will transactions and roll-backs be handled, then in Service Layers?

like image 939
Akhil K Nambiar Avatar asked Oct 19 '25 03:10

Akhil K Nambiar


1 Answers

The javadoc explains it:

This filter makes Hibernate Sessions available via the current thread, which will be autodetected by transaction managers. It is suitable for service layer transactions via HibernateTransactionManager or JtaTransactionManager as well as for non-transactional execution (if configured appropriately).

NOTE: This filter will by default not flush the Hibernate Session, with the flush mode set to FlushMode.NEVER. It assumes to be used in combination with service layer transactions that care for the flushing: The active transaction manager will temporarily change the flush mode to FlushMode.AUTO during a read-write transaction, with the flush mode reset to FlushMode.NEVER at the end of each transaction. If you intend to use this filter without transactions, consider changing the default flush mode (through the "flushMode" property).

like image 112
JB Nizet Avatar answered Oct 21 '25 15:10

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!