Performance oriented Spring Data JPA & Hibernate by Maciej Walkowiak

Learn essential Spring Data JPA & Hibernate optimization techniques including fetch strategies, connection management, transaction handling, and query performance tuning.

Key takeaways
  • Turn off spring.jpa.open-in-view by default in Spring Boot applications to prevent holding database connections open unnecessarily

  • Change fetch type to LAZY for @ManyToOne and @OneToMany relationships instead of using default EAGER loading

  • Use projections instead of fetching full entities when you only need specific fields, especially for read operations

  • Monitor database connection usage with tools like Flexy, P6Spy, or DataSource-proxy to detect connection leaks and diagnose N+1 query problems

  • Avoid occupying database connections during external service calls by structuring transactions appropriately

  • Use @DynamicUpdate to only update changed columns instead of all columns when modifying entities

  • Consider using Spring’s TransactionTemplate for fine-grained transaction control instead of @Transactional with nested transactions

  • Implement proper connection pool sizing as the default of 10 connections can become a bottleneck

  • Use getReference() instead of findById() when you only need entity references without needing to load data

  • Be explicit about joins using @EntityGraph or JPQL join fetch statements to avoid N+1 queries

  • Consider using native SQL queries or JDBC for complex read operations where JPA mappings add unnecessary overhead