Let's say I have a scheduled job that regularly runs a database modification over a list of entities, all in one transaction. Depending on the situation, there might be lots to modify, or nothing at all.
entityManager.getTransation().begin();
for (MyEntity e : myEntityRepository.getAllDirtyEntities()) {
myEntityRepository.cleanUp(e); // does some modification to e
}
entityManager.getTransaction().commit();
What is the best way to deal with scenarios where getAllDirtyEntities() returns nothing (= an empty List)?
commit()anyway, even though nothing was changedrollback(), even though nothing was changed- neither of the two: do nothing; let the
entityTransactionget GC'd - Avoid this scenario completely: query first,
begin()&commit()the transaction only if there is something to do