-->
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.  [ 8 posts ] 
Author Message
 Post subject: two open transactions for one session
PostPosted: Tue Nov 22, 2005 7:46 am 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
Can I have two open transactions at the same time for one session? Is there some problem with this?
If it's possible, which operations is related with each transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 3:18 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Ronaldo, it is possible indeed. But it may give you some problems.

There are many hibernate docs that encourage you to follow the "one session, one transaction" rule. If you don't, you may have problems like the one below:
http://www.hibernate.org/206.html

Take a look at what is says.

Well, that's all I know.

Até mais pra vc :p

_________________
Don't forget to rate if the post helped!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 3:26 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
Which operations is related with each transaction?
For example:
if the sessionA has two open transactions T1 and T2.
If I do session.save(), session.load and other operations, how can I indicate which transaction the operation must me associated?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 3:34 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
The thing is it doesn't really let you use both transactions, having full control of them.

For instance, you may have something like this:

Code:
    Transaction tx0 =session.beginTransaction();
    session.saveOrUpdate(myObj);
    Transaction tx1 =session.beginTransaction();
    session.saveOrUpdate(myObj1);
    tx0.commit();
    tx1.commit();


Both times you call session.saveOrUpdate will commit only the first transaction. It will always refer to the first one. When you do tx0.commit, it will commit the first transaction again. Only when you do tx1.commit, it will commit the second transaction.

_________________
Don't forget to rate if the post helped!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 3:44 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
moonlight wrote:
Transaction tx0 =session.beginTransaction();
session.saveOrUpdate(myObj);
Transaction tx1 =session.beginTransaction();
session.saveOrUpdate(myObj1);
tx0.commit();
tx1.commit();


There is no need to call saveOrUpdate ... automatic dirty checking will take care of this once commit is fired.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 3:55 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
This will also depend on the TransactionFactory you use.

If you use a JTA evironment, the method beginTransaction() will give you a new Transaction if no one exists. But if you've already started a transaction it will return the existing transaction. :)

I.E: http://www.hibernate.org/hib_docs/v3/ap ... ction.html
Quote:
Implements a basic transaction strategy for JTA transactions. Instances check to see if there is an existing JTA transaction. If none exists, a new transaction is started. If one exists, all work is done in the existing context. The following properties are used to locate the underlying UserTransaction:


I don't think you should even want to use 2 transactions at the same time. Two in a sequence is possible, but parallel lacks of good code design imo. ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 4:05 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
I don't understand. Your saying auto - dirty checking behaves differently in jta environments? or that saveOrUpdate would be needed in such an environment?

and yes, i agree there are few use cases that require the 2 transactions.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 4:28 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
dennisbyrne wrote:
I don't understand. Your saying auto - dirty checking behaves differently in jta environments? or that saveOrUpdate would be needed in such an environment?

No, I mean that the use of 2 parallel transactions depends also on the TransactionFactory implementation that is used. Some will support it, some don't (like JTA) :)
Quote:
and yes, i agree there are few use cases that require the 2 transactions.

I never needed 2 or more transactions parallel on the same session.
2 or more transactions sequencial I can imagine, but parallel.. no ;)


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