许多MySQL连接

seb*_*ian 6 java mysql hibernate

我有点迷失这个事实:

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 |
+-----------------------------------+-------+
Run Code Online (Sandbox Code Playgroud)

我在这里读了一些类似的问题,但那些不是我的问题,所以我在这里.

我使用MySQL和Hibernate.在我的webapp中,有一个静态的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);
  }

}
Run Code Online (Sandbox Code Playgroud)

然后我像在这个例子中一样调用util类:

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

所以基本上我关闭成功连接(closeSession)和错误(errorSession).现在为什么我在MySQL控制台中看到这么多连接?

Fra*_*ero 3

其意义connections并非如你所想。正如文档 connections中所说的意思是:

尝试连接 MySQL 服务器的次数(成功或失败)。

所以你并不像你想象的那样有 10535 个活跃连接。