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

The other issue was that a transaction was not active for the rollback, as
the commit will do a rollback if it fails.  The solution to that issue it to
check the transaction state.

It seems your issue has nothing to do with this if you are getting your
database committed.  This means that either your commit was successful, or
that you database is not supporting transactions, or that you have JTA
configured incorrectly.  What is your persistence.xml and your database?



saty15 wrote:
> 
> I have same problem. I did rollback in catch block, but data inserted in
> DB.
> 
>  catch (RuntimeExcep tion e) {
>             try {
>                em.getTransaction().rollback();  // After commit fail or
> some other error need to rollback manually
>             } catch (RuntimeException e) {
>                // Log rollback failure or something
>             }
> 
> 
> 
> 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 (RuntimeExcep tion 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://old.nabble.com/Rollback-after-failed-commit-tp23314454p28972006.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top