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 question和yet another SO question,我已经确保batch_size
设置在application.properties
文件中,尽管我没有父子关系,但我也尝试使用order_inserts
和order_updates
。此外,我还启用了rewriteBatchedStatements=true
选项并使用了saveAll(...)
方法
我还试图通过在每次{
上面没有任何帮助
# 1 楼答案
以下属性在Spring Boot中不存在
要添加自定义属性,请改用
spring.jpa.properties
前缀我们应该做到这一点
另请参见how to configure JPA上的Spring引导文档