Spring Boot 3.3.0集成PostgreSQL 16批量插入失效排查求助
PostgreSQL批量插入不生效问题排查(Spring Boot 3.3.0 + PostgreSQL 16)
在Spring Boot 3.3.0 + PostgreSQL 16环境下尝试实现批量插入,该功能在MySQL中可正常运行,但PostgreSQL中始终是逐条执行插入语句,已排查多日未解决,请求帮忙定位代码或配置中的问题。
application.yml配置
spring: jpa: show_sql: true hibernate: ddl-auto: create properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect generate_statistics : true order_inserts: true order_updates: true jdbc: batch_size: 10 batch_versioned_data: true datasource: url: jdbc:postgresql://127.0.0.1:5432/batch?rewriteBatchedInserts=true username: postgres password: driver-class-name: org.postgresql.Driver # hikari: # data-source-properties: # rewriteBatchedInserts: true
实体类与业务逻辑
实体类
@Entity @Getter @Table(name = "bacnet") @NoArgsConstructor public class BacnetEntity { @Id @GeneratedValue(generator = "uuid2") @GenericGenerator(name = "uuid2", strategy = "uuid2") private UUID id; private int deviceId; private int objectType; private int objectId; public BacnetEntity(int deviceId, int objectType, int objectId) { this.deviceId = deviceId; this.objectType = objectType; this.objectId = objectId; } }
业务代码
@Transactional void run() { List<BacnetEntity> list = new ArrayList<>(); for (int i = 0; i < 30; i++) { BacnetEntity bacnetEntity = new BacnetEntity(i, i, i); list.add(bacnetEntity); } bacnetRepository.saveAll(list); }
Hibernate会话统计信息
2024-06-08T23:26:59.923+09:00 INFO 28496 --- [ main] i.StatisticalLoggingSessionEventListener : Session Metrics { 546000 nanoseconds spent acquiring 1 JDBC connections; 0 nanoseconds spent releasing 0 JDBC connections; 5137900 nanoseconds spent preparing 1 JDBC statements; 0 nanoseconds spent executing 0 JDBC statements; 6477700 nanoseconds spent executing 3 JDBC batches; 0 nanoseconds spent performing 0 L2C puts; 0 nanoseconds spent performing 0 L2C hits; 0 nanoseconds spent performing 0 L2C misses; 35687800 nanoseconds spent executing 1 flushes (flushing a total of 30 entities and 0 collections); 0 nanoseconds spent executing 0 pre-partial-flushes; 0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections) }
PostgreSQL日志信息(已设置log_statement='all'、log_duration=on)
2024-06-08 23:37:53.371 KST [11932] LOG: execute S_2: insert into bacnet (device_id,object_id,object_type,id) values ($1,$2,$3,$4) 2024-06-08 23:37:53.371 KST [11932] DETAIL: parameters: $1 = '22', $2 = '22', $3 = '22', $4 = 'd5bbd137-368a-4c51-aef8-d77c8c7251ce' 2024-06-08 23:37:53.371 KST [11932] LOG: duration: 0.041 ms 2024-06-08 23:37:53.371 KST [11932] LOG: duration: 0.002 ms 2024-06-08 23:37:53.371 KST [11932] LOG: execute S_2: insert into bacnet (device_id,object_id,object_type,id) values ($1,$2,$3,$4) 2024-06-08 23:37:53.371 KST [11932] DETAIL: parameters: $1 = '23', $2 = '23', $3 = '23', $4 = '90771e48-5bc2-4b3b-8c80-ca3e8ae63fc6' 2024-06-08 23:37:53.371 KST [11932] LOG: duration: 0.039 ms 2024-06-08 23:37:53.371 KST [11932] LOG: duration: 0.002 ms 2024-06-08 23:37:53.371 KST [11932] LOG: execute S_2: insert into bacnet (device_id,object_id,object_type,id) values ($1,$2,$3,$4) 2024-06-08 23:37:53.371 KST [11932] DETAIL: parameters: $1 = '24', $2 = '24', $3 = '24', $4 = '4ed99923-9666-4e3a-983b-b8b4486572bd' 2024-06-08 23:37:53.371 KST [11932] LOG: duration: 0.042 ms 2024-06-08 23:37:53.371 KST [11932] LOG: duration: 0.002 ms 2024-06-08 23:37:53.372 KST [11932] LOG: execute S_2: insert into bacnet (device_id,object_id,object_type,id) values ($1,$2,$3,$4) 2024-06-08 23:37:53.372 KST [11932] DETAIL: parameters: $1 = '25', $2 = '25', $3 = '25', $4 = 'caed3524-7d77-41ef-a9e4-614e2b629620' 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.048 ms 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.002 ms 2024-06-08 23:37:53.372 KST [11932] LOG: execute S_2: insert into bacnet (device_id,object_id,object_type,id) values ($1,$2,$3,$4) 2024-06-08 23:37:53.372 KST [11932] DETAIL: parameters: $1 = '26', $2 = '26', $3 = '26', $4 = 'd9ee937e-437b-45aa-a258-1feba3c157b8' 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.046 ms 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.003 ms 2024-06-08 23:37:53.372 KST [11932] LOG: execute S_2: insert into bacnet (device_id,object_id,object_type,id) values ($1,$2,$3,$4) 2024-06-08 23:37:53.372 KST [11932] DETAIL: parameters: $1 = '27', $2 = '27', $3 = '27', $4 = 'fd9d969e-a4e0-434e-9648-a377e8a276e8' 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.043 ms 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.002 ms 2024-06-08 23:37:53.372 KST [11932] LOG: execute S_2: insert into bacnet (device_id,object_id,object_type,id) values ($1,$2,$3,$4) 2024-06-08 23:37:53.372 KST [11932] DETAIL: parameters: $1 = '28', $2 = '28', $3 = '28', $4 = 'b8ec0431-1cde-42f8-8b2c-915fde9a1c3b' 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.041 ms 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.003 ms 2024-06-08 23:37:53.372 KST [11932] LOG: execute S_2: insert into bacnet (device_id,object_id,object_type,id) values ($1,$2,$3,$4) 2024-06-08 23:37:53.372 KST [11932] DETAIL: parameters: $1 = '29', $2 = '29', $3 = '29', $4 = 'bd81419f-8c85-4ce0-89e9-eda534ec15ef' 2024-06-08 23:37:53.372 KST [11932] LOG: duration: 0.042 ms 2024-06-08 23:37:53.375 KST [11932] LOG: duration: 0.019 ms 2024-06-08 23:37:53.375 KST [11932] LOG: duration: 0.011 ms 2024-06-08 23:37:53.375 KST [11932] LOG: execute S_3: COMMIT
求助需求
希望找到在PostgreSQL中实现批量插入的有效方法,排查当前代码或配置存在的问题。
内容的提问来源于stack exchange,提问作者이상찬
相关产品推荐
相关产品推荐

