Skip to main content

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

JPA does require commit() to rollback on failure. RollbackException means it
did a rollback.

The technically correct error handling code is:

        em.getTransaction().begin();
        try {
            ...
            em.getTransaction().commit();
        } catch (RuntimeException e) {
            if (em.getTransaction().isActive()) {
                try {
                   em.getTransaction().rollback();
                } catch (RuntimeException e) {
                   // Log rollback failure or something
                }
            }
            throw e;
        } finally {
             em.close();
        }


Syvalta wrote:
> 
> 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();
>         }
> 
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Rollback-after-failed-commit-tp23314454p23317019.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top