Spring Boot Application批处理在JpaRepository.saveAll方法中不起作用

问题描述

有一个配置为spring boot的应用程序:

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}

我使用JpaRepository.saveAll方法将新数据插入数据库。 但是我看不到该批处理有效。

有日志:

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)
}

版本:

  1. spring-data-jpa-2.2.6。发布
  2. postgresql-42.2.12

解决方法

批处理不适用于GenerationType.IDENTITY

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

需要使用顺序。

示例:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "row_generator")
@SequenceGenerator(name = "row_generator",sequenceName = "db_generator",allocationSize = 1)
private Long id;
,

几周前,我遇到了类似的问题。 我现在简单地从事务内部为所有实体一个一个地调用save(...),它可以按预期工作。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...