Gautam Kumar’s Post

🚀 How a Single Annotation Made Our Java Backend 50x Faster Sometimes, performance issues hide in plain sight. In our case, it was a seemingly harmless @Transactional annotation. Here’s what happened 👇 We had: @Transactional @Query("SELECT u FROM User u WHERE u.id = :id") Optional<User> findById(@Param("id") Long id); This annotation was silently creating proxies, starting unnecessary transactions, and performing dirty checks — all for a simple read query. The fix? @Transactional(readOnly = true) public interface UserRepository extends JpaRepository<User, Long> {} 💡 Instant impact: 10ms → 0.2ms per query (50x faster!) 📊 Key takeaways: Use @Transactional(readOnly = true) for queries — avoids unnecessary flush checks Don’t annotate repository methods with @Transactional unless needed Always profile before guessing — tools like JProfiler, YourKit, or async-profiler reveal hidden bottlenecks Micro-optimizations scale — saving milliseconds per request can mean hours of CPU time saved daily Sometimes, small changes lead to massive performance wins. ⚡ #Java #SpringBoot #Performance #BackendDevelopment #CodeOptimization #TechLearning #SpringDataJPA

Thanks for sharing , I like your content. Now, the @ Transactional(readOnly=true) is usless on jpa repos because it is a default thing. The base class of the generated proxy for your repository is SimpleJpaRepository which is transactional read only by design, which makes all your repo's methods transactional read only too. The problem you made here is that you've overwrote this behavior with @ Transactional at your query method level. For more details check : - SimplaJpaRepository.java - JpaRepositoryFactory.java - RepositoryFactorySupport.java

To view or add a comment, sign in

Explore content categories