-->
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.  [ 7 posts ] 
Author Message
 Post subject: Multi-transaction usage pattern
PostPosted: Fri Jul 16, 2004 6:53 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 11:01 pm
Posts: 23
What is the proper usage pattern for multiple transactions in a session?

Is it:

Code:
session = sessionFactory.getSession();
transaction = session.beginTransaction();
... some stuff ...
transaction.commit();
... some stuff ...
transaction.commit();
session.close();


Or:

Code:
session = sessionFactory.getSession();
transaction = session.beginTransaction();
... some stuff ...
transaction.commit();
transaction = session.beginTransaction();
... some stuff ...
transaction.commit();
session.close();



Thanks,
-Mark


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 7:21 pm 
Newbie

Joined: Fri Jul 16, 2004 6:22 pm
Posts: 17
I'm new to Hibernate, but for some reason I was under the impression that transactions were supposed to used 1 to 1 with a session. In other words, a new session should be created per transaction. This view could certainly be wrong, and if it is, someone please correct me.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 7:24 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, there are several patterns. The Session acts as a "persistence context", this is useful for long running "application transactions". There is a page on the Wiki/Community Area that describes the session and transaction scopes. If you need more detaild information about application design with a persistence context, read Hibernate in Action.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 12:39 am 
Beginner
Beginner

Joined: Wed Oct 01, 2003 11:01 pm
Posts: 23
Christian, I've read the wiki page but it does not answer my question. Perhaps I've mislabeled my message, but all I'm asking for is if I need to start a new transaction after every commit, or if I can just keep working in the current transaction.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 2:10 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
unkyaku wrote:
Christian, I've read the wiki page but it does not answer my question. Perhaps I've mislabeled my message, but all I'm asking for is if I need to start a new transaction after every commit, or if I can just keep working in the current transaction.

Thanks.


Are you trying to save 1 line of code, or are you just trying to write Java like you would sql, or is there another reason? Whether you can save this line or not, I would keep it in (start a new transaction). Based on the API, even if it happens to work now without starting a transaction, I would think you are safer to start it anyway in case things change in future releases of Hibernate (someone correct me if Im wrong, I didnt check the source code):

http://www.hibernate.org/hib_docs/api/n ... ml#commit()

http://www.hibernate.org/hib_docs/api/n ... ansaction()

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 2:14 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
firelord wrote:
I'm new to Hibernate, but for some reason I was under the impression that transactions were supposed to used 1 to 1 with a session. In other words, a new session should be created per transaction. This view could certainly be wrong, and if it is, someone please correct me.


This is definitely wrong. Check here for many design strategies on transactions and sessions:

http://www.hibernate.org/168.html

What you describe is one option if it is conducive to your app, but you dont have to do it that way...

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 3:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Get Hibernate in Action, this stuff is a main theme of the book.


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