jeudi 5 mars 2015

How to handle many jpa entities in one request (OpenSessionInView)

we have classic Spring application with OpenSessionInView pattern.


Sometimes we want proces many (=unknown size) entities in one request. This is well-known OutOfMemory issue.


Long time ago, we create function(hack) to switch current hibernate session for particular method.



public void proceedOnePerson(int id) {
recalculateVAT(id);
}

public void proceedAllPerson(int[] ids) {
for(int id : ids) {
switchToAnotherHibernateSesion();
recalculateVAT(id); //OutOfMemory
closeAnotherSessionAndSwitchBackToOriginSession();
}
}


Is there standard solution to this in Spring, Spring Boot or Spring Data?



  • Session.clear is not suitable, because has side effect on other entities from other methods.

  • Session.evict is not suitable too, because developer of method recalculateVAT had no idea if this function will be used in batch. Error prone.

  • Spring batch is too heavy. And we dont want to write one method for user and second method for batch.


Aucun commentaire:

Enregistrer un commentaire