-->
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: Session not closing properly?
PostPosted: Sun Feb 28, 2010 7:07 pm 
Newbie

Joined: Thu Feb 25, 2010 11:09 am
Posts: 7
Hi, I just got hibernate working but I have a problem. It only works for a while then I get the "Cannot open connection".
I can only assume that the sessions are not closing and finally it gets too many?
I made a simplified example to illustrate.

Code:
public class HQLGroupByExample {
   
   public static void main(String[] args) {
      for (int i=0; i<125; i++){
         func();
      }
   
   }
   
   public static void func(){
      Session session = null;
      try {
         // This step will read hibernate.cfg.xml and prepare hibernate for
         // use
         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
         session = sessionFactory.openSession();
         //Group By Clause Example
          Transaction tx = session.beginTransaction();
         String SQL_QUERY = "select sum(insurance.investementAmount),insurance.insuranceName "
            + "from Insurance insurance group by insurance.insuranceName";
         Query query = session.createQuery(SQL_QUERY);
         for (Iterator it = query.iterate(); it.hasNext();) {
            Object[] row = (Object[]) it.next();
            //   System.out.println("Invested Amount: " + row[0]);
            System.out.println("Insurance Name: " + row[1]);
         }
          tx.commit();
         
      } catch (Exception e) {
         System.out.println(e.getMessage());
      } finally {
         session.close();
      }
   }



The loop works for maybe a hundred runs or so and then it doesn't.
Anyone knows how to make it work?


Top
 Profile  
 
 Post subject: Re: Session not closing properly?
PostPosted: Mon Mar 01, 2010 3:39 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Which session-pool implementation are you using ?
c3p0 or other?


Top
 Profile  
 
 Post subject: Re: Session not closing properly?
PostPosted: Mon Mar 01, 2010 8:44 am 
Newbie

Joined: Thu Feb 25, 2010 11:09 am
Posts: 7
pb00067 wrote:
Which session-pool implementation are you using ?
c3p0 or other?


Hi, thanks for the reply.
I have to admit this is the first I have heard of this.
I just toke a simple example from http://www.roseindia.net/hibernate/index.shtml.
I was hoping it would just be a matter of fixing the code a little to make the sessions close or something but I guess it wasn't that simple.
I will look into session-pooling and c3p0. If you have any more suggestions please let me know.


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.