2015-10-13 6 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のアクティブな接続を持っていません。

SHOW STATUS LIKE 'Threads_connected'; 
+0

私愚か: – sebastian

1

は、実際の接続のスレッドが使用参照してください。ありがとうございました!
+0

ありがとうございます。 81の接続がもっと良いようです:-) – sebastian