-->
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: Postgres is leaving too many open sessions
PostPosted: Tue Jul 15, 2008 11:43 pm 
Newbie

Joined: Tue Jul 15, 2008 11:09 pm
Posts: 2
Hi all,

I'm a beginner in hibernate and I am working with Postgres, my problem is that aparently hibernate is leaving a lot of open sessions to Postgres. I realized that when I went to Postgres Pgadmin-tools-server status, this is showing me a lot of PID all of them with Query=<IDLE>.

The way I'm using hibernate is basically:

SessionFactory sessionFactory = ht.getSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("from TABLE where id = XXX");
myResult = query.list();
session.getTransaction().commit();
session.disconnect();
session.close();


Any help would be great.

_________________
Best regards,
CRODv


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 6:10 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You definitely don't want to be creating quite so many SessionFactory objects, that's for sure.

The other thing I'd say is that you probably want getSession, not openSession. There's a big difference! getSession is typically the right choice, unless you want to manage the state of the session yourself.

I've got a good tutorial on using the HibernateUtil class. You might want to create one of your own and use it:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=05samplehibernateutilclassexample

Code:
package com.examscam;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import com.examscam.model.User;

public class HibernateUtil {

private static SessionFactory factory;

public static Configuration 
             getInitializedConfiguration() {
   AnnotationConfiguration config =
               new AnnotationConfiguration();
/* add all of your JPA annotated classes here!!! */
   config.addAnnotatedClass(User.class);
   config.configure();
   return config;
}

public static Session getSession() {
   if (factory == null) {
   Configuration config = 
      HibernateUtil.getInitializedConfiguration();
   factory = config.buildSessionFactory();
   }
   Session hibernateSession =
                   factory.getCurrentSession();
   return hibernateSession;
}

public static void closeSession() {
   HibernateUtil.getSession().close();
}


public static void recreateDatabase() {
   Configuration config;
   config =
       HibernateUtil.getInitializedConfiguration();
   new SchemaExport(config).create(true, true);
}

public static Session beginTransaction() {
   Session hibernateSession;
   hibernateSession = HibernateUtil.getSession();
   hibernateSession.beginTransaction();
   return hibernateSession;
}

public static void commitTransaction() {
   HibernateUtil.getSession()
                .getTransaction().commit();
}

public static void rollbackTransaction() {
   HibernateUtil.getSession()
                 .getTransaction().rollback();
}

  public static void main(String args[]) {
    HibernateUtil.recreateDatabase();
  }

}


http://www.thebookonhibernate.com/HiberBookWeb/learn.jsp?tutorial=05samplehibernateutilclassexample

You might also want to look at this quick tutorial on How Hibernate Works to get a better understanding of what's going on in your code:




http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=07howhibernateworks

_________________
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: Sounds good
PostPosted: Tue Jul 22, 2008 11:08 pm 
Newbie

Joined: Tue Jul 15, 2008 11:09 pm
Posts: 2
Really makes sense your suggestion, I will try that, I guess that you mean method getCurrentSession().

I'll let you know how it goes.

Thanks!

BTW: We ordered your hibernate book!

_________________
Best regards,
CRODv


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 3:00 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Fantastic! Thanks for the order. :) Make sure you get Bauer and Kings book too. It's the reference.

And yes, getCurrentSession can make all the difference!

-Cameron McKenzie

_________________
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  
 
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.