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.  [ 5 posts ] 
Author Message
 Post subject: Session not closing properly?
PostPosted: Sun Feb 28, 2010 7:13 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 1:27 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Hmm, do not call SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory() many times, just load it first time. Try to call getCurrentSession() instead of openSession().


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

Joined: Thu Feb 25, 2010 11:09 am
Posts: 7
SIau_Tie wrote:
Hmm, do not call SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory() many times, just load it first time. Try to call getCurrentSession() instead of openSession().



Hi thanks for your reply.
I accidently posted this twice and now I got replies in both threads.
Anyway I will look into your reply too =)


Strangely enough that methods seems to be missing for me. It says
"The method getCurrentSession() is undefined for the type SessionFactory"

OK, wrong version of hibernate3.jar. When I changed it for a newer one, it did find getCurrentSession() but none of the examples work.
They just say

Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.HQLGroupByExample.func(HQLGroupByExample.java:56)
at roseindia.tutorial.hibernate.HQLGroupByExample.main(HQLGroupByExample.java:28)

Does anyone know exactly which jars I need to make it work and where to get them?
I went to https://www.hibernate.org/6.html but there are just so overwhelmingly many different downloads there and not much information about what is what as far as I can see.
Anyone? I'm really confused about this.


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

Joined: Thu Feb 25, 2010 11:09 am
Posts: 7
Ok, I downloaded hibernate-distribution-3.3.2.GA

Made a new project in eclipse and added the jars under
hibernate-distribution-3.3.2.GA\lib\required

slf4j-api-1.5.8.jar
jta-1.1.jar
javassist-3.9.0.GA.jar
dom4j-1.6.1.jar
commons-collections-3.1.jar
antlr-2.7.6.jar

and I also added

hibernate3.jar

I have the following example code that worked with the old jars.

Code:
package roseindia.tutorial.hibernate;



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
   public static void main(String[] args) {
      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();
            //Create new instance of Contact and set values in it by reading them from form object
             System.out.println("Inserting Record");
             Transaction tx = session.beginTransaction();
             Contact contact = new Contact();
             contact.setFirstName("Arun");
             contact.setLastName("Jain");
             contact.setEmail("akj_lh@yahoo.co.in");
             session.save(contact);
             tx.commit();
            System.out.println("Done");
      }catch(Exception e){
         System.out.println(e.getMessage());
      }finally{
         // Actual contact insertion will happen at this step
      //   session.flush();
      //   session.close();

         }
      
   }
}


And get the following response

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:24)
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 8 more

Any ideas?


Top
 Profile  
 
 Post subject: Re: Session not closing properly?
PostPosted: Mon Mar 01, 2010 1:04 pm 
Newbie

Joined: Thu Feb 25, 2010 11:09 am
Posts: 7
OK got it to work!
(added a file called slf4j-nop-1.5.8.jar as well)


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