-->
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.  [ 2 posts ] 
Author Message
 Post subject: MySQL Connection Leak
PostPosted: Mon Dec 20, 2010 12:39 pm 
Newbie

Joined: Mon Dec 20, 2010 12:31 pm
Posts: 1
I am very new to Hibernate. I get the error below if I refresh my jsp page a few times.

Quote:
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Communications link failure during rollback(). Transaction resolution unknown.


I guess I don't close session properly. Here is the HibernateUtil class and how I use it. Please help check it and let me know if I have done anything wrong. Really appreciate your help.

Code:
public class HibernateUtil { //copied from official Hibernate3 tutorial.

    public static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            // Store it in the ThreadLocal variable
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s != null)
            s.close();
        session.set(null);
    }
}


How I use it
Code:
org.hibernate.Session ss = HibernateUtil.currentSession();
Transaction tx = ss.beginTransaction();

ArrayList<SpecialAccount> accounts = null;
try{
   if (newRequest.updateDB()){
      owner = newRequest.getOwner();
      if(owner != null && owner.getCamsID()<1) owner.updateDB();
      newRequest.setMachines(machines);
      if(newRequest.checkAccountNameConflict()) throw new HibernateException("Account name already exists.");
      accounts = newRequest.generateAccounts();
                accounts.updateDB();
         
   }
   tx.commit();
   HibernateUtil.currentSession().flush();
   
}
catch(Exception e){
   if (tx != null) tx.rollback();
   return;
}
finally{
   HibernateUtil.closeSession();
}


Top
 Profile  
 
 Post subject: Re: MySQL Connection Leak
PostPosted: Thu Dec 23, 2010 2:27 am 
Newbie

Joined: Tue Dec 21, 2010 8:21 am
Posts: 5
we see this error when hi-frequency queries get slow, and Tomcat can't put the connection back in the pool fast enough (or its other threads). Then Tomcat has to keep adding other DB connections until it exceeds the mysql max_connections

for the reference just have a look at the following post

http://forums.mysql.com/read.php?39,181 ... msg-226710


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