The advantages of transactions are known as ACID: Atomicity, Consistency, Isolation, and Durability. In methods like save and update, transactions are effective because they support dirty checking and various other features.
Also, when setting readonly to true, JPA recognizes that entities within that transaction are intended for read-only purposes, and it does not maintain separate snapshots for change detection, thus saving memory and providing performance benefits.
@Transactional(readOnly = true)
public Optional<Adate> getAdateByCalendarId(String calendarId) {
return adateRepository.findAdateByCalendarId(calendarId);
}
However, I've come to think that for reads (R) in CRUD operations, excluding CUD (Create, Update, Delete), it might not be necessary to use @Transactional(readonly=true).
Here's a brief explanation for the reasons:
- Adding @Transactional(readonly=true) to simple queries can be considered over-engineering.
- I understand that entities retrieved during queries are stored in the first-level cache, but there's no need to store them in the first-level cache.
Have a nice day🙂 - kevin