-->
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.  [ 6 posts ] 
Author Message
 Post subject: Transaction rollback
PostPosted: Mon Sep 27, 2004 1:53 pm 
Newbie

Joined: Wed Jul 28, 2004 1:02 pm
Posts: 9
I have a need to update one table based on previous transaction result. However, if the update failed, the previous transaction should rollback. I have the following code trying to do so but it does not seen to work. Any help is appreciated.
------------------------------

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);
}

}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 27, 2004 4:44 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
You can't do a rollback after a commit, it's too late. If you need the save() inside to be rolled back when the update() fails, use a single transaction around both.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 27, 2004 5:40 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: Wed Sep 29, 2004 12:24 am 
Newbie

Joined: Wed Jul 28, 2004 1:02 pm
Posts: 9
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 had the JDBCTransaction manager:

hibernate.transaction.factory_class=net.sf.hibernate.transaction.JDBCTransactionFactory

Do I need to change the following : ??

hibernate.transaction.factory_class=net.sf.hibernate.transaction.JTATransactionFactory

-------------
How do I do any nested transaction ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 3:39 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Quote:
How do I do any nested transaction ?


As far as I know, JDBC doesn't support nested transactions at all.
In your example, you have nested transactions since you create two transactions from the same Hibernate session - and therefore the same JDBC connection (one per session). The 2nd transaction is then nested inside the first ;)


Changing to the JTA transaction is meaningfull only if you have a JTA transaction manager... most probably inside a full-blown app-server like JBoss.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 10:04 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
realskywalker wrote:
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 think you should look for another way to autoincrement the value that now incremented by your trigger via sequence number. In your case the two operations should be inside one transaction and you don't need any nested one.

What value do you need to autoincrement? If it is some property of the first object (tk), try this, it should work:
Code:
// insert a new row in Table1

tk = something ....

session.save(tk);
session.flush();
session.refresh(tk); // re-reads tk with updated property

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

tkper.setTKId(tk); // some property of tk was changed by trigger and then re-read via session.refresh()
session.update(tkper);

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

_________________
Leonid Shlyapnikov


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.