Spring Boot MySQL不是批处理插入

问题描述 投票:-1回答:1

我已经在这里发布了关于这个问题的所有解决方案,但没有任何效果。

我的application.properties:

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?logger=com.mysql.jdbc.log.Slf4JLogger&rewriteBatchedStatements=true&profileSQL=true&autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.hibernate.jdbc.batch_size = 100
spring.jpa.hibernate.order_inserts   = true 
spring.jpa.hibernate.order_updates   = true

logging.level.root=info
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5p) %gray(%c{0}::%M) - %m%n

我的EntityRepository:

@Repository
public interface EntityRepository extends CrudRepository<Entity, Long> { }

我的实体:

@Data @Entity
public class Entity {

  @Id
  @GeneratedValue(generator = "generator")
  @GenericGenerator(name = "generator", strategy = "increment")
  private Long id;

  private Long attr;

}

以及调用存储库的简化代码:

int batchSize = 100;
List<Entity> batchEntities = new ArrayList<>();
for(Entity entity : entities) {
  batchEntities.add(entity);
  if(batchEntities.size() >= batchSize) {
    entityRepository.saveAll(batchEntities);
    batchEntities.clear();
  }
}

我做了什么:

根据this SO question,我启用了profileSQL=true选项,日志生成了几个单独的插入。此外,我已在SQL服务器上启用全局日志记录,它也会生成单个插入序列。

根据this another SO questionyet another SO question,我确保batch_size设置在application.properties文件中,虽然我没有亲子关系,但我也尝试使用order_insertsorder_updates。另外,我启用了rewriteBatchedStatements=true选项并使用saveAll(...)方法。

我也试图抛弃预制的CrudRepository和我的定制的batchSize-persist后冲洗。

上面没有任何帮助。

java mysql spring hibernate spring-boot
1个回答
1
投票

Spring Boot中不存在以下属性。

spring.jpa.hibernate.jdbc.batch_size = 100
spring.jpa.hibernate.order_inserts   = true 
spring.jpa.hibernate.order_updates   = true

要添加自定义属性,请使用spring.jpa.properties前缀。

spring.jpa.properties.hibernate.jdbc.batch_size = 100
spring.jpa.properties.hibernate.order_inserts   = true 
spring.jpa.properties.hibernate.order_updates   = true

应该做的伎俩。

另请参阅how to configure JPA上的Spring Boot文档。

© www.soinside.com 2019 - 2024. All rights reserved.