mardi 5 mai 2015

Jpa and rollback: a pattern to preseve entity consistency

Consider the following scenario where Jpa is used for persistence. A student can be associated to different courses with a web form. So this form displays different entities (student, course).

The Save button is pushed, the business logic modify some fields of the entities, but the db operation fails.

Unfortunately the enities in memory reflects the changes made by the business logic and this may create some inconsistency problem. Is there a pattern useful in similar scenarios ?

Possible solution I thought and why I don't like them:

  • Don't want to revert back all the changes made the bussiness logic in case of db exception becuase it is an error prone job.
  • Don't want to reload the entities after the db exception in order to be sure their are aligned with the db. In fact this operation may fail too.

Otherwise I can clone the entities, make the changes and swap the clone with the original entity after a successful commit.

Anyway I would be more confortable following a well established pattern.

Aucun commentaire:

Enregistrer un commentaire