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.  [ 3 posts ] 
Author Message
 Post subject: [Newbie] Replacing JDBC with Hibernate
PostPosted: Tue Jul 22, 2008 5:23 am 
Newbie

Joined: Wed Feb 14, 2007 4:49 am
Posts: 7
Dear Hibernate users,
I'm going to replace a JDBC layer with Hibernate. I have tested the basic Hibernate examples now I'd like to know if I can avoid istantiating all Hibernate objects for every query/insert.

For example:
Is it good practice to put in a Singleton the SessionFactory and Configuration

Code:
public getInstance() {
                              if (config == null) {
                    config = new Configuration();
                              }
                           
                             if (sessionFactory == null) {
                    sessionFactory = config.buildSessionFactory();
                             }
}


Also, is there any advantage to keeping the Session object at Class level so that I don't open a new one for every query ?

Code:
public void getConnection {
           if (session == null) {
              session = sessionFactory.openSession();
          }
          else {
             return session;
          }
}


Thanks for your help
Francesco


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 22, 2008 5:58 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You are definitely in the right directions.

The Session Factory is a very heavyweight objects, so you want to instantiate it in a Singleton type manner and make sure your application is only using one per database.

Sessions themselves are fairly lightweight though. You shouldn't have any guilty feelings about calling getSession whenever a Hibernate3 Session is needed.

By the way, feel free to check out my signature links for some further tutorials on how to use Hibernate. You might find them useful.

Hope this helps!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 22, 2008 6:07 am 
Newbie

Joined: Wed Feb 14, 2007 4:49 am
Posts: 7
thanks a lot for the precise answer and for the links provided.
Regards
Francesco


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