-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Transaction rollback
PostPosted: Mon Sep 27, 2004 1:50 pm 
Newbie

Joined: Wed Jul 28, 2004 1:02 pm
Posts: 9
Hibernate version:2.01

Mapping documents:

Code between sessionFactory.openSession() and session.close():

// get a hiberate open session

Session session = sessionFactory.openSession();

Transaction t1 = session.beginTransaction();
Transaction t2 = session.beginTransaction();
try
{
// insert a new row in Table1

tk = something ....

session.save(tk);
session.flush();
t1.commit();


// Based on new information of tk, update Table2
// but if update failed, I want to roll back Table1 transaction.

tkper.setTKId(tk);
session.update(tkper);

session.flush();
t2.commit();
session.close();

}
catch (HibernateException e)
{
t1.rollback(); // does not seen to work.
t2.rollback();
logger.error("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
finally
{
if (session != null)
{
try
{

session.close();
}
catch (HibernateException e)
{
logger.error("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}

}
}



Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 6:22 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
If I understand you correctly, you want the update in transaction t1 to be rolled back if an error is generated in the following section of code:

Code:
tkper.setTKId(tk);
session.update(tkper);

session.flush();
t2.commit();


Since you have already successfully committed the transaction t1 before you even execute the code above, then it is too late to try to rollback on transaction t1 if there is an error in transaction t2.

In your example, you'd want to move the t1.commit(); call to after the t2.commit() call. However, you could then get the situtation where the commit to t2 succeeds and the commit to t1 fails. If you want both transactions to succeed or both to fail, then what you really need is a two-phase commit.

_________________
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 7:01 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
If you are using the JDBCTransactionManager - thus not using JTA - you cannot have nested transactions.

As I can see, you two transactions come from the same Hibernate session, and therefore the same JDBC connection...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 11:28 pm 
Newbie

Joined: Wed Jul 28, 2004 1:02 pm
Posts: 9
Thanks for the help guys.

The reason that I have to commit the first transaction is that it does not seen to give the lastest sequence number (DB trigger generated) of Table1 unless it is commited. This sequence number is needed for the second transaction.

I guess I have to use JTA then. Thanks for the help !!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.