-->
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: Good Hibernate Usage for a fairly simple web application
PostPosted: Mon Mar 30, 2009 6:04 pm 
Newbie

Joined: Mon Mar 30, 2009 5:48 pm
Posts: 6
Hibernate version: Hibernate 3

Name and version of the database you are using: MYSQL 5.0.67


Hello, I searched around the net and had trouble answering my questions so I though I would start a thread.

Basically I read the hibernate.org/42 thread and found it very interesting and got me wondering about how I would like to use hibernate in my application.

My application is a simple web app which places database information in tables on a web page, or inserts user information into the database and produces artifacts at the user request. Basically I was reading the above web page and the Transaction demarcation with plain JDBC section interested me. I was wondering what detailed information people could give me regarding

Code:
try {
    factory.getCurrentSession().beginTransaction();

    // Do some work
    factory.getCurrentSession().load(...);
    factory.getCurrentSession().persist(...);

    factory.getCurrentSession().getTransaction().commit();
}
catch (RuntimeException e) {
    factory.getCurrentSession().getTransaction().rollback();
    throw e; // or display error message
}


vs.

Code:
Session session = factory.openSession();
Transaction tx = null;
try {
    tx = session.beginTransaction();

    // Do some work
    session.load(...);
    session.persist(...);

    tx.commit(); // Flush happens automatically
}
catch (RuntimeException e) {
    tx.rollback();
    throw e; // or display error message
}
finally {
    session.close();
}




I am particularly interested in hearing what circumstances would lead me to choose one over the other?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 5:08 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
it's almost the same code the only real difference is in these lines

Code:
Session session = factory.openSession();


Code:
factory.getCurrentSession().beginTransaction();


The second line is correct but the way it's coded is actually just confusing code. We should not chain method calls like this it makes code harder to understand.

If I was to write that example myself this is what it would look like

Code:
Session session = factory.getCurrentSession();
Transaction tx = session.beginTransaction();


so essentially the new version above is the same as the second example just more readable.
Code:
factory.getCurrentSession().beginTransaction();


You can now see they are both getting a session and a transaction. Now the actual difference is that factory.openSession() always returns a new session. Where as factory.getCurrentSession() will return the current session or a new one if one is not present.

Other than that difference they are the same exact code. One is just poorly written.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 5:13 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
oh yeah and your first example could also include these lines of code. If you want to make sure your session is closed after the transaction. You only close the session if you no longer want to reuse it. That's up to you to figure out depending on the specifics of your app.


Code:
} finally {
    session.close();
}

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 1:15 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
ethnarch wrote:
it's almost the same code the only real difference is in these lines

Not reallly!!! As the page itself says:
factory.getCurrentSession() - gives back you the session that is associated with the current context which in most of the case is the current thread. So in a single thread you get the same session object for all the getCurrentSession calls. The most important point is that this session will be managed by hibernate. So you dont have to open and close the session yourself. The first call will open the session and the transaction.commit() will close it.

But factory.openSession() - opens a new Connection and a new Session. This session is fully your responsibility. So hibernate will not close the session when transaction is commited. So you should make sure that session is closed properly at the end.

So if you dont want to manage the session yourself then you can use getCurrentSession() and if for some reason you want to manage it yourself then you use openSession().

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 3:14 am 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
Have you considered using Spring to manage hibernate? It makes things a lot easier and decisions like these are taken care of for you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 8:12 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
littypreethkr
Quote:
Not reallly!!! As the page itself says:

Oh come on your going to falt me for not copying and pasteing the whole page. I even gave him the links to read. I did say "almost" the same code.


But I appreciate you pointing that out.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 12:43 pm 
Newbie

Joined: Mon Mar 30, 2009 5:48 pm
Posts: 6
Thanks guys, very helpful I have been learning alot, if you guys could help answer my question about SessionFactory I would be much appreciative.

http://forum.hibernate.org/viewtopic.ph ... highlight=


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.