【问题标题】:Spring Boot Appllication. Batch doesn't work in JpaRepository.saveAll methodSpring Boot 应用程序。批处理在 JpaRepository.saveAll 方法中不起作用
【发布时间】:2020-12-29 00:59:18
【问题描述】:

有配置的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.RELEASE
  2. postgresql-42.2.12

【问题讨论】:

  • 请检查这是否有帮助https://stackoverflow.com/questions/27697810/hibernate-disabled-insert-batching-when-using-an-identity-identifier-generator/27732138

标签: spring postgresql hibernate jpa spring-data-jpa


【解决方案1】:

批处理不适用于 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;

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,但有不同的说法。批处理是一个包含许多操作的语句。
    • Hibernate 会将分开的保存命令合并成批次。
    猜你喜欢
    • 2018-08-23
    • 2017-07-28
    • 2018-03-15
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 2016-12-22
    相关资源
    最近更新 更多