2015-10-13 88 views
6

我有點失去了這個事實:許多MySQL連接

show status like 'con%'; 

+-----------------------------------+-------+ 
| Variable_name      | Value | 
+-----------------------------------+-------+ 
| Connection_errors_accept   | 0  | 
| Connection_errors_internal  | 0  | 
| Connection_errors_max_connections | 0  | 
| Connection_errors_peer_address | 0  | 
| Connection_errors_select   | 0  | 
| Connection_errors_tcpwrap   | 0  | 
| Connections      | 10535 | 
+-----------------------------------+-------+ 

我讀到這裏一些類似的問題,但在這些情況下,問題在那裏不是我的,所以我在這裏。

我使用MySQL和Hibernate。在我的web應用程序有這個靜態的HibernateUtil類來訪問數據庫:

import org.apache.log4j.Logger; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

public class HibernateUtil { 

    private static final Logger log = Logger.getLogger(HibernateUtil.class);   
    private static SessionFactory sessionFactory;  

    static { 

     try { 
      sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); 
     } catch (Throwable ex) { 
      // error handling 
     } 
    } 

    public static final ThreadLocal session = new ThreadLocal(); 

    public static Session currentSession() throws HibernateException { 

     Session s = null; 
     try { 
      s = (Session) session.get(); 
     } catch(Exception e) { 
      // error handling 
     } 

     // Open a new Session, if this thread has none yet 
     if (s == null) { 
      try { 
       s = sessionFactory.openSession(); 
      } catch(Exception e) { 
       // error handling 
      } 

      try { 
       s.getTransaction(); 
      } catch(Exception e){ 
       // error handling 
      } 
      Transaction tx = null; 

      while(tx==null){ 
       try { 
        tx = s.beginTransaction(); 
        // Store it in the ThreadLocal variable 
       } catch(Exception j) { 
        // error handling 
       } 
      } 
      session.set(s);   
     } 
     return s; 
    } 


public static void closeSession() throws HibernateException { 
    Session s = (Session) session.get(); 
    if (s != null){ 
     try { 
      s.getTransaction().commit(); 
      s.close(); 
     } catch(Exception e) { 
      // error handling 
     } 
    } 
    session.set(null); 
    } 

public static void errorSession() throws HibernateException { 
    Session s = (Session) session.get(); 
     try { 
      s.getTransaction().rollback(); 
      s.close(); 
     } catch(Exception e) { 
      // error handling 
     } 
    session.set(null); 
    } 

} 

然後我打電話的Util類就像這個例子:

private MyTable getMyTable() { 
    try { 
     Session session = currentSession(); 
     // some prepared statement 
     return myTable; 
    } catch (HibernateException e) { 
     errorSession(); 
     return null; 
    } finally { 
     closeSession(); 
    } 
} 

所以基本上我關上成功的連接(closeSession)和錯誤(errorSession)。 現在爲什麼我會在MySQL控制檯中看到如此多的連接?

回答

3

connections的含義並非您所想的。

連接嘗試(成功與否)MySQL服務器的數目: 如docsconnections裝置所述。

因此,您認爲沒有10535個活動連接。

+0

愚蠢的我。謝謝! – sebastian

1

要看到實際的連接線程使用:

SHOW STATUS LIKE 'Threads_connected'; 
+0

謝謝。 81開放連接看起來好多了:-) – sebastian