Batching database writes in Spring

Spring JDBC Template v/s Spring Data JPA: Comparison of work involved for implementing a persistence layer

Create entity and repository

Introduce Batching

spring.jpa.properties.hibernate.jdbc.batch_size = 10

Improve logging

spring.jpa.show-sql = true
spring.jpa.properties.hibernate.generate_statistics = true

Behavior of batch insert operations

  1. The inserts got batched
  2. For every INSERT statement, there is also an additional SELECT statement being executed
  1. Use a common base class which has a transient flag defaulting to indicate a new instance.
  2. Use JPA lifecycle callbacks “@PrePersist”and “@PostLoad” to flip that flag on persistence operations. The flag must get flipped to indicate an existing entity after a repository call to save()

Batching UPDATE queries

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store