-->
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: ISession / ITransaction Best Practice
PostPosted: Sat Oct 08, 2005 10:56 am 
Beginner
Beginner

Joined: Fri May 13, 2005 11:48 am
Posts: 32
Code:
using(ISession sess = GetASessionSomeHow())
using(ITransaction tr = sess.BeginTransaction())
{
  try{
    ...do stuff [UnitOfWork?]
    tr.Commit();
  }catch (Exception ex){
    tr.Rollback();
    ...do failure stuff
  }
}


How does this look for a simple best practice use of ISession and ITransaction?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 12:41 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
It looks fine :)

You may add
Code:
catch{
    tr.Rollback();
    ...do failure stuff
}

for non-CSLCompliant exceptions :wink:

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: Re: ISession / ITransaction Best Practice
PostPosted: Wed Dec 14, 2005 1:53 pm 
Newbie

Joined: Wed Sep 14, 2005 12:01 am
Posts: 3
Location: Latvia
dsellers wrote:
Code:
using(ISession sess = GetASessionSomeHow())
using(ITransaction tr = sess.BeginTransaction())
{
  try{
    ...do stuff [UnitOfWork?]
    tr.Commit();
  }catch (Exception ex){
    tr.Rollback();
    ...do failure stuff
  }
}


How does this look for a simple best practice use of ISession and ITransaction?


Hibernate documentation says:
Quote:
If you rollback the transaction you should immediately close and discard the current session to ensure that Hibernate's internal state is consistent.


Hibernate recommended exception handling:
Code:
Session sess = factory.openSession();
Transaction tx = null;
try {
    tx = sess.beginTransaction();
    // do some work
    ...
    tx.commit();
}
catch (Exception e) {
    if (tx!=null) tx.rollback();
    throw e;
}
finally {
    sess.close();
}


Isn't it also a best practice for NHibernate?
Shouldn't we also close session after transaction commit/rollback?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 11:50 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Complete explanation here: NHibernateEg.Tutorial1A - Persistence API

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.