I have a SpringBoot/Spring Data JPA application. Until recently, we used the default Spring setting "open-in-view". I had to switch this setting off, because as the use cases were going more complexe, managing transactions became a nightmare. Switching the setting off helped indeed getting the control back on transactions.
This has a price though: we have suddenly to deal with some LazyInitializationExceptions. This was expected. But I'd like to know if my way of dealing with these exceptions is ok. In most of the cases we get LazyInitializationException while reading data: the code tried to access nested properties of detached entities. My (lazy?) way of dealing with this situation is to add a
@Transactional(readOnly=true)
in the concerned service methods. Is there anything wrong with this approach?
I'm wondering because in my readings about this topic
https://vladmihalcea.com/the-best-way-to-handle-the-lazyinitializationexception/
https://thorben-janssen.com/lazyinitializationexception/
redesigning the scope of the transactions is never mentioned, when it seems to me the easiest and fastest fix.