即使应用程序已经关闭,HikariCP连接仍在运行中运行

时间:2018-08-30 05:12:00

标签: java mysql hikaricp

即使我已经在Chrome中停止了该应用程序,HikariCp连接仍在日食中运行。我不确定是什么问题。有时控制台会向我发出警告:

  

为com.mysql.jdbc.JDBC4Connection@2f9642f6触发了连接泄漏检测,随后是堆栈跟踪    接着   先前报告的泄漏连接com.mysql.jdbc.JDBC4Connection@2f9642f6已返回到池中(未泄漏)

这是我的hikari代码

package com.dbConn;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.sql.Connection;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class Conn {

    private HikariDataSource ds;
    private Connection c;
    private Statement s;

    public static final boolean LOG = true;

    private boolean SHW_LOG = LOG;

    private boolean multiStatement = false;

    private List<Statement> statementLst;

    public DataSource getDS(){

        return ds;

    }

    public void setDS(ServletContextEvent event){
        //config CP
        try{

        ServletContext sc = event.getServletContext();
        HikariConfig config = new HikariConfig();  
        config.addDataSourceProperty("serverName","localhost");
        config.addDataSourceProperty("port", 3308);
       config.addDataSourceProperty("databaseName", "esys");
        config.addDataSourceProperty("user", "root");
        config.addDataSourceProperty("password", "admin");
        config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); 
        config.addDataSourceProperty("cachePrepStmts", true);  
        config.addDataSourceProperty("prepStmtCacheSize", 500);  
        config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);  
        config.setMinimumIdle(0);
        config.setMaximumPoolSize(20);
        config.setAutoCommit(true);  
        config.setIdleTimeout(180000);
        config.setMaxLifetime(1800000);
        config.setInitializationFailFast(true);
        config.setConnectionTestQuery("SELECT current_timestamp");
        ds = new HikariDataSource(config); 
        sc.setAttribute("db", ds);

        } catch (Exception e){
            e.printStackTrace();
        }   
    }




    public void shutdown(){
        ds.close();
    }

    public void close(){
        try {
            if (this.s != null) {   //if statement exist
                try {
                    this.s.close(); //close statement
                } catch (Exception e) {
                }
                this.s = null;      // let statement = null
            }

        } finally {
            if (this.c != null) {   //if connection exist
                try {
                    this.c.close();  //close connection
                } catch (Exception e) {
                }
                this.c = null;       // let connection = null
            }
        }
    }

    public Connection getConnection(){

        if(getDS()!= null){
            try {  

                return getDS().getConnection();

            }catch (SQLException e) {  
                e.printStackTrace();  
            //should be ds.resumePool(), not shutdown() or close()??
                ds.resumePool();  
                return null;  
            }  
        }else{

            try {

                return ds.getConnection();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        //System.out.println("CONNECTION2");
    }

    public ResultSet execSQL(String sql) throws SQLException {
        // Execute a sql
        Statement s1 = null;
        if (multiStatement) {
            s1 = getConnection().createStatement();
            if (statementLst == null) {
                statementLst = new ArrayList<Statement>();
            }
            statementLst.add(s1);
        } else {
            if (s == null) {
                s = getConnection().createStatement();
            }
            s1 = s;
        }
        if (isSHW_LOG()) {
            System.out.println("Execute = " + sql);
        }
        return s1.executeQuery(sql);
    }

    public boolean isSHW_LOG() {
        return SHW_LOG;
    }

    public void setSHW_LOG(boolean shw_log) {
        SHW_LOG = shw_log;
    }

    public boolean isMultiStatement() {
        return multiStatement;
    }

    public void setMultiStatement(boolean multiStatement) {
        this.multiStatement = multiStatement;
    }

    public PreparedStatement prepareStatement1(String sql, String[] a)
            throws SQLException {
        if (isSHW_LOG()) {
            System.out.println("Prepare = " + sql);
        }
        return getConnection().prepareStatement(sql,
                com.mysql.jdbc.PreparedStatement.RETURN_GENERATED_KEYS);
    }

    public PreparedStatement prepareStatement2(String sql, int returnKeys)
            throws SQLException {
        if (isSHW_LOG()) {
            System.out.println("Prepare = " + sql);
        }
        return getConnection().prepareStatement(sql, returnKeys);
    }

}

0 个答案:

没有答案