gpt4 book ai didi

transactions - Spring 数据 JPA : manual commit transaction and restart new one

转载 作者:行者123 更新时间:2023-12-05 01:10:27 26 4
gpt4 key购买 nike

我有一种导入数据的方法。如果导入很大,它不能在单个事务中运行,而不会由于巨大的事务语句缓存而导致 OutOfMemoryError。

我想要的是在语句缓存中的 n 条记录后手动提交。

我怎样才能做到这一点? (最好在@Transactional 方法中)。

最佳答案

使用 EntityManager.flush()EntityManager.clear()在每次第 N 次迭代时,使 session 同步到数据库,并清除 chache 以防止 OOM。

如果您使用 Hibernate,您还可以设置 hibernate.jdbc.batch_size到适当的值以在 JDBC 级别进行批处理。

如果您还想在批处理后提交并坚持使用@Transactional 方法,那么请重构您的代码,以便@Transactional 方法从输入源获取一批N 条记录,并从外部循环调用该方法。否则你可以使用 Spring 的 TransactionTemplate以编程方式控制事务。

这可能有用:http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch15.html

关于transactions - Spring 数据 JPA : manual commit transaction and restart new one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com