-->
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.  [ 2 posts ] 
Author Message
 Post subject: nested transactions
PostPosted: Wed Jun 01, 2005 5:41 am 
Newbie

Joined: Wed Jun 01, 2005 5:34 am
Posts: 6
Location: Karlsruhe (Germany)
We have the following code:

Session session = currentSession();
Transaction tx1 = session.beginTransaction();

TrackableLink newTL = new TrackableLink();
newTL.setUrl( "innen "+new java.util.Date().toString());
newTL.setMailingID( 1);
session.save( newTL);

Transaction tx2 = session.beginTransaction();

newTL = new TrackableLink();
newTL.setUrl( "außen "+new java.util.Date().toString());
newTL.setMailingID( 1);
session.save( newTL);

tx1.commit();
tx2.rollback();

closeSession();


After execution we have two new rows in the database.
When when commiting tx2 after a rollback of tx1 we get no new rows.

Can we use multiple transactions in a single session? Our example seams to support just one.
How are the transactions handled exactly.


Hibernate 3, Oracle 9


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 11:44 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Well - I'm not the expert at this may help:

From the API Docs:
Quote:
The lifecycle of a Session is bounded by the beginning and end of a logical transaction


Only a single transaction can therefore be bound to a session at any one time. Otherwise you would need to say:
Code:
session.save(Object, TransactionToSaveIn)

as opposed to
Code:
session.save(Object)


The behaviour when opening a second nested transaction is therefore undefined in line with your results.

Note that you could perform multiple NON-NESTED transactions within the scope of a single session i.e.:
Code:
Session sess = factory.openSession();
Transaction tx;
try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();


     tx = sess.beginTransaction();
     //do some more work
     ...
     tx.commit();


}
catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
}
finally {
     sess.close();
}

[/b]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.