Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Rollback after failed commit

I have always thought that the following is a correct usage pattern in JPA in
Java SE mode:

        em.getTransaction().begin();
        try {
            entity.setProperty("test");
            em.getTransaction().commit();
        } catch (RuntimeException e) {
            try {
               em.getTransaction().rollback();  // After commit fail or some
other error need to rollback manually
            } catch (RuntimeException e) {
               // Log rollback failure or something
            }
            throw e;
        } finally {
             em.close();
        }
However, em.rollback() causes "java.lang.IllegalStateException: Exception
Description: No transaction is currently active.", provided exception is
thrown during commit.

JPA spec says that "transaction must marked for rollback" for most
PersistenceExceptions. Additonally, javadoc for commit() says "Commit the
current transaction, writing any unflushed changes to the database. throws:
RollbackException - if the commit fails." So nothing about the transaction
is actually rolled back.

Is my original interpretation incorrect, and the more proper pattern would
be something like this:
        try {
           em.getTransaction().begin();
           entity.setProperty("test");
           em.getTransaction().commit();  // Will rollback automatically if
commit fails?
        } finally {
          em.close();
        }


-- 
View this message in context: http://www.nabble.com/Rollback-after-failed-commit-tp23314454p23314454.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top