Skip to main content

JPA and Rollbacks - Not Pretty

Posted by davidvc on April 27, 2007 at 5:03 PM PDT

We all want our interactions with the database to be successful. And most demos and code samples have everything going hunky dory. But what happens if they're not? What if you or the database needs to roll back the transaction? With JPA, if you're not careful, things can get pretty ugly.

Here it is from the horse's mouth:


3.3.2 Transaction Rollback

For both transaction-scoped and extended persistence contexts,
transaction rollback causes all pre-existing managed instances and
removed instances to become detached. ... Transaction rollback typically causes the persistence
context to be in an inconsistent state at the point of rollback. In
particular, the state of version attributes and generated state (e.g.,
generated primary keys) may be inconsistent. Instances that were
formerly managed by the persistence context (including new instances
that were made persistent in that transaction) may therefore not be
reusable in the same manner as other detached objects

Comments

One note to lazy loading: EclipseLink JPA provider can ...

One note to lazy loading: EclipseLink JPA provider can handle lazy loading on detached objects - it automatically creates short-lived transaction to initialize.