我已经在这里发布了关于这个问题的所有解决方案,但没有任何效果。
我的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 question和yet another SO question,我确保batch_size
设置在application.properties
文件中,虽然我没有亲子关系,但我也尝试使用order_inserts
和order_updates
。另外,我启用了rewriteBatchedStatements=true
选项并使用saveAll(...)
方法。
我也试图抛弃预制的CrudRepository
和我的定制的batchSize
-persist后冲洗。
上面没有任何帮助。
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文档。