Return to site

⚡ Make @Transactional(readOnly = true) Actually Fast (Vlad Java Champion Way)

· java

1/ @Transactional(readOnly = true) in Spring (before 5.1) is mostly a hint, not a speed boost.

2/ It gets faster only when Hibernate knows the Session is really read-only.

3/ Extend HibernateJpaDialect and override it so it calls session.setDefaultReadOnly(true) for @Transactional(readOnly = true) transactions.

4/ Marking the Hibernate Session as read-only turns off dirty checking → less work, less CPU, faster queries.

5/ Use it for reports, dashboards, and read-heavy APIs.

6/ Don’t do updates inside read-only transactions—your changes may be ignored.

#JavaChampion 🏆 #VladMihalcea #SpringBoot #SpringData #Transactional #ReadOnlyTransaction #Hibernate #JPA #JavaPerformance #Microservices #CleanArchitecture #BackendDevelopment