spring data如何清理事务方法中的持久实体?

Art*_*nov 5 java spring hibernate transactional spring-data

我需要使用 spring data 通过 hibernate 接收和保存大量数据。我们的服务器分配的 RAM 不足以同时保存所有实体。我们肯定会得到 OutOfMemory 错误。

所以我们需要批量保存数据,这是显而易见的。此外,我们还需要使用 @Transactional 来确保所有数据都保留或不保留,即使发生单个错误。

那么,问题是: @Transactional 方法期间的 spring 数据是否继续将实体存储在 RAM 中,或者垃圾收集器可以访问刷新的实体?

那么,使用 Spring Data 处理大量数据的最佳方法是什么?也许 Spring Data 不是解决此类问题的正确方法。

Ken*_*han 6

@Transactional 方法期间的 Spring 数据是否继续将实体存储在 RAM 中,或者垃圾收集器可以访问已刷新的实体?

实体将继续存储在RAM中(即在entityManager)中,直到事务提交/回滚或entityManager被清除。entityManager.clear()这意味着只有在事务提交/回滚或被调用时,实体才有资格进行 GC 。

那么,使用 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<Foo> 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();
    }
} 
Run Code Online (Sandbox Code Playgroud)

请注意,这种做法只是为了防止 OOM,而不是为了获得高性能。因此,如果您不关心性能,则可以安全地使用此策略。