有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring引导MySQL不批处理插入

关于这个问题,我已经应用了这里发布的几乎所有的解决方案,但是没有任何效果

我的应用程序。属性

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

我的实体记述

@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 server上启用了全局日志记录,它也会生成单个插入的序列

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

我还试图通过在每次{}后刷新来丢弃预制的{}和自定义的

上面没有任何帮助


共 (1) 个答案

  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引导文档