许多MySQL连接

6

我对这个事实有点困惑:

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控制台中看到这么多连接?
2个回答

3

connections的含义并非你所想的那样。如文档所述,connections的意思是:

连接到MySQL服务器的尝试次数(无论成功与否)。

因此,你并没有像你想的那样有10535个活动连接。


我真是太傻了。谢谢! - sebastian

1
要查看实际的连接线程,请使用:

SHOW STATUS LIKE 'Threads_connected';

谢谢。81个打开的连接看起来好多了 :-) - sebastian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接