-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate is not releasing mysql connection..
PostPosted: Sat Jan 18, 2014 6:30 am 
Newbie

Joined: Sat Jan 18, 2014 5:55 am
Posts: 1
Hello all,

I am new to hibernate. I am creating some basic hibernate program to doing some insert into MySql database. The below is the main code I have written. Although I am closing the session after transaction and seeing that " Released JDBC connection" on console, I can see an inactive connection in the database after running the program.Everytime I am runnning the programm a new inactive connection is getting created in the database. I have to click "terminate" button in eclips console to stop that inactive session everytime.

Code:
[color=#404080]
Student s1=new Student();
   s1.setName("Monaj");
   s1.setRoll(1);
   s1.setDate(new Date());

Configuration configuration=new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);
   
Session session=sessionFactory.openSession();
Transaction t1=session.beginTransaction();
session.save(s1);
t1.commit();
session.close();[/color]



Console output:-
15:29:08.476 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Aggressively releasing JDBC connection
15:29:08.476 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Releasing JDBC connection
15:29:08.476 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Released JDBC connection
15:29:08.477 [main] DEBUG o.h.e.j.internal.JdbcCoordinatorImpl - HHH000420: Closing un-released batch
15:29:37.323 [pool-1-thread-1] DEBUG o.h.e.j.c.i.DriverManagerConnectionProviderImpl - Connection pool now considered primed; min-size will be maintained


Hibernate Configuration:-

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.password">password</property>
<property name="connection.username">admin</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="connection.release_mode">AFTER_TRANSACTION</property>
<property name="connection.autocommit">true</property>

Could anyone please help me on this.


Top
 Profile  
 
 Post subject: Re: Hibernate is not releasing mysql connection..
PostPosted: Sun Jul 13, 2014 11:39 pm 
Newbie

Joined: Sun Jul 13, 2014 11:36 pm
Posts: 3
Is there a solution for these issue?


Top
 Profile  
 
 Post subject: Re: Hibernate is not releasing mysql connection..
PostPosted: Mon Jul 14, 2014 9:53 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
To terminate Hibernate you have to close the SessionFactory.

Otherwise managed connection pools will do their job by design: try and keep a pool of connections open to reuse. This is essential to achieve good performance.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate is not releasing mysql connection..
PostPosted: Mon Jul 14, 2014 6:41 pm 
Newbie

Joined: Sun Jul 13, 2014 11:36 pm
Posts: 3
Thanks Sanne,

I was checking out documentation to try understand how hibernate works. In my situation, I use hibernate in a JPA example. The next example code is working with Eclipselink and Datanucleus.

Code:

package cl.molavec.jpa.tests;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cl.molavec.jpa.entities.QUser;

public class Example01 {
   
     private static final String PERSISTENCE_UNIT_NAME = "jpa_test01";
     private static EntityManagerFactory factory;    
     private static Logger logger = LoggerFactory.getLogger(Example01.class);

     public static void main(String[] args) {
      
       factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
       EntityManager em = factory.createEntityManager();
       // read the existing entries and write to console
      
      
      
       // create new todo
       em.getTransaction().begin();
       QUser user = new QUser();
       user.setUsername("User13");
       em.persist(user);
       em.getTransaction().commit();

       Query q = em.createQuery("select t from QUser t");
       List<QUser> userList = q.getResultList();
      
       for (QUser usern : userList) {
         System.out.println(usern);
       }
      
       System.out.println("Size: " + userList.size());
      
       em.close();
    
       logger.info("------------> End Program");
     }
}




With Hibernate I has the problem showed above. My problem is how to get Session object (I think this should be the object to close).

I other way, I tried to add the next property in persistence.xml, but it doesn't make any difference:

Code:
<property name="hibernate.connection.release_mode" value="on_close" />


Is possible to include those properties in this file or it should be in hibernate.cfg.xml.

Thanks in advance.


Last edited by molavec on Mon Jul 14, 2014 7:08 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Hibernate is not releasing mysql connection..
PostPosted: Mon Jul 14, 2014 6:54 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you don't need to set such properties, as for example "connection.release_mode" applies to the end of the specific Session or the specific EntityManager instance.

What you need to do is - when your program is done - to close the SessionFactory or the EntityManagerFactory (depending if you're using the Hibernate classical interface or the JPA interface)

So in your case:

Code:
factory.close();

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate is not releasing mysql connection..
PostPosted: Mon Jul 14, 2014 7:24 pm 
Newbie

Joined: Sun Jul 13, 2014 11:36 pm
Posts: 3
Sanne,

That was fast! Thank you very much this works!


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