Reputation: 1353
There is spring boot application with configuration:
spring:
jpa:
show-sql: true
properties:
hibernate:
jdbc:
batch_size: 20
order_inserts: true
order_updates: true
generate_statistics: true
format_sql: true
dialect: org.hibernate.dialect.PostgreSQL10Dialect
default_schema: ${SCHEMA}
I use JpaRepository.saveAll method for inserting new data into DB. But I don't see that batching working.
There is logs:
Session Metrics {
1501300 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
13234500 nanoseconds spent preparing 1013 JDBC statements;
1636949500 nanoseconds spent executing 1013 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
12971600 nanoseconds spent executing 1 flushes (flushing a total of 1012 entities and 0 collections);
4901200 nanoseconds spent executing 2 partial-flushes (flushing a total of 4 entities and 4 collections)
}
Versions:
Upvotes: 1
Views: 3210
Reputation: 1353
Batch doesn't work with GenerationType.IDENTITY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Need to use sequence.
Example:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "row_generator")
@SequenceGenerator(name = "row_generator", sequenceName = "db_generator", allocationSize = 1)
private Long id;
Upvotes: 5
Reputation: 3423
I had a similar problem a few weeks ago. I simply call save(...) one by one for all the entities from inside a transaction now, and it works as expected.
Upvotes: 1