java永久存储数据_java-Spring数据如何清除事务方法中的永久实体?

Does spring data during @Transactional method keep storing entities in

RAM or entities which were flushed are accessible to garbage

collector?

实体将继续存储在RAM中(即,在entityManager中),直到清除事务提交/回滚或entityManager.这意味着只有在事务提交/回滚或

调用entityManager.clear().

So,what is the best approach to process huge mount of data with

spring data?

防止OOM的一般策略是逐批加载和处理数据.在每个批次的末尾,您应该刷新并清除entityManager,以便entityManager可以释放其用于CG的托管实体.常规代码流应如下所示:

@Component

public class BatchProcessor {

//Spring will ensure this entityManager is the same as the one that start transaction due to @Transactional

@PersistenceContext

private EntityManager em;

@Autowired

private FooRepository fooRepository;

@Transactional

public void startProcess(){

processBatch(1,100);

processBatch(101,200);

processBatch(201,300);

//blablabla

}

private void processBatch(int fromFooId,int toFooId){

List foos = fooRepository.findFooIdBetween(fromFooId,toFooId);

for(Foo foo :foos){

//process a foo

}

/*****************************

The reason to flush is send the update sql to DB .

Otherwise,the update will lost if we clear the entity manager

afterward.

******************************/

em.flush();

em.clear();

}

}

请注意,此做法仅用于防止OOM,而不能用于实现高性能.因此,如果您不关心性能,则可以安全地使用此策略.@H_301_9@

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值