-->
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.  [ 1 post ] 
Author Message
 Post subject: which one between these two coding style(using session)?
PostPosted: Mon Oct 16, 2006 10:53 pm 
Newbie

Joined: Tue Feb 21, 2006 2:00 am
Posts: 14
Location: Seoul, Korea
Sorry for my poor english^^;

multithreaded evnvironment.

about 15 java application using hibernate session.

all java files have code like this.

Code:
session sess = null;
Transaction tx = null;

//some hibernate concern code 1
try{
    sess = HibernateUtil.openSession();
    tx = sess.beginTransaction();
    ....
}
catch(HibernateException e){
    if( tx != null) tx.rollback();
    session.close();
    return;
}

// some plain java code 1 - business logic - long line
....
....

//some hibernate concern code 2
try{
    sess.createQry..
    ....
    sess.update..
}
catch(HibernateException e){
    if( tx != null) tx.rollback();
    session.close();
    return;
}

// some plain java code 2 - business logic - long line
....
....


//some hibernate concern code 3
try{
    sess.createQry..
    ....
    sess.update..
    sess.save..
}
catch(HibernateException e){
    if( tx != null) tx.rollback();
    session.close();
    return;
}

// some plain java code 3 - business logic - long line
....
....
....
//some hibernate concern code n
try{
    sess.createQry..
    ....
    sess.update..
    sess.save..
    tx.commit();
}
catch(HibernateException e){
    if( tx != null) tx.rollback();
}
finally{
    session.close();
}


Is it right?

I think following is right usage.

Code:
session sess = null;
Transaction tx = null;

try{
    sess = HibernateUtil.openSession();
    tx = sess.beginTransaction();
   
    //some plain java code 1
    // it might be long and have other try catch clause
    ....
    ....
    sess.update..
    .....
    //some plain java code 1
    // it might be long and have other try catch clause
    ....
    ....
    sess.save..
    .....
    //some plain java code 2
    // it might be long and have other try catch clause
    ....
    ....
    sess.delete..
    .....
    //some plain java code n
    // it might be long and have other try catch clause
    ....
    ....
    sess.update..
    .....
catch(HibernateException e){
    if( tx != null) tx.rollback();
}
finally{
    session.close();
}


which one must I choose?

thanks in advance..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.