Соединение HikariCP все еще работает в Eclipse, хотя приложение уже закрыто - PullRequest
0 голосов
/ 30 августа 2018

HikariCp соединение все еще работает в затмении, хотя я уже остановил приложение в Chrome. Я не уверен, в чем проблема. Иногда консоль давала мне предупреждение:

Обнаружение утечки соединения запущено для com.mysql.jdbc.JDBC4Connection@2f9642f6, трассировка стека следует а потом Ранее сообщалось, что утечка соединения com.mysql.jdbc.JDBC4Connection@2f9642f6 была возвращена в пул (не пропущена)

Вот мой код для хикари

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);
    }

}
...