2016-03-25 48 views
0

我觀察到的數據庫連接不會被關閉我的MYSQL提示符下運行命令爲什麼數據庫連接不被在這種情況下關閉

show status like 'Conn%'; 

的連接都保存在增加

這是怎麼我想提出的數據庫連接

package com.conn; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.naming.InitialContext; 
import javax.naming.NamingException; 
import javax.sql.DataSource; 

import org.apache.log4j.Logger; 


public class DBConnection { 
    final static Logger logger = Logger.getLogger(DBConnection.class); 
private static DataSource dataSource; 
    static { 
      try { 
        dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/MYDATABASE"); 
      } catch (NamingException e) { 
       try { 
        throw new Exception("'jndifordbconc' not found in JNDI",e); 
       } catch (Exception e1) { 
        logger.error(e1); 
       } 
      } 
     } 





public static Connection getDBConnection() { 
     try { 
      return dataSource.getConnection(); 
     } catch (SQLException e) { 
       logger.error(e); 
      return null; 
     } 

    } 




    public static void close(Connection con) 
    { 
     if (con != null) 
     { 
      try 
      { 
       con.close(); 
      } 
      catch (SQLException e) 
      { 
        logger.error(e); 
      } 
     } 
    } 

    public static void close(Statement stmt, ResultSet rs) 
    { 
     if (rs != null) 
     { 
      try 
      { 
       rs.close(); 
      } 
      catch (SQLException e) 
      { 
        logger.error(e); 
      } 
     } 
     if (stmt != null) 
     { 
      try 
      { 
       stmt.close(); 
      } 
      catch (SQLException e) 
      { 
        logger.error(e); 
      } 
     } 
    } 
} 

<Resource name="jdbc/MYDATABASE" 
     auth="Container" 
     type="javax.sql.DataSource" 
     driverClassName="com.mysql.jdbc.Driver" 
     url="jdbc:mysql://localhost:3306/Test?allowMultiQueries=true" 
     factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" 
    username="root" 
     password="[email protected]" 
    initialSize="5" 
     maxActive="25" 
     maxIdle="10" 
    minIdle="10" 
    defaultTransactionIsolation="READ_COMMITTED" 
    suspectTimeout="60" 
    timeBetweenEvictionRunsMillis="3400" 
    minEvictableIdleTimeMillis="5500" 
    validationQuery="SELECT 1" 
    validationInterval="3400" 
    testOnBorrow="true" 
    removeAbandoned="true" 
    removeAbandonedTimeout="55" 
    jmxEnabled = "true" 
    closeMethod="close" 
     /> 

這是我怎麼想提出的連接和關閉連接

dbConnection = DBConnection.getDBConnection();

public class AutofillArea { 
    final static Logger logger = Logger.getLogger(AutofillArea.class); 
    @GET 
    @Consumes("application/text") 
    @Produces("application/json") 
    public String getData(
      @QueryParam("city") String city , 
      @QueryParam("area") String area 

      ) 
    { 
     city = Utility.getProperString(city); 
     area = Utility.getProperString(area); 

     String response =" "; 
     Connection dbConnection = null; 
     PreparedStatement statePreparedStmt = null; 
     ResultSet stateResultSet = null; 
     JSONArray jsonarray = new JSONArray(); 
     try 
     { 
      String sql = "select distinct area from mytable where city = ?;"; 
      dbConnection = DBConnection.getDBConnection(); 
      statePreparedStmt = dbConnection.prepareStatement(sql); 
      statePreparedStmt.setString(1 ,city); 
      stateResultSet = statePreparedStmt.executeQuery(); 
      while(stateResultSet.next()) 
      { 
       jsonarray.put(stateResultSet.getString("area")); 
      } 
      response = "jsonCallbackarea("+jsonarray.toString()+")"; 
     } 
     catch(Exception e) 
     { 
      logger.error(e); 
     } 

     finally 
     { 
      try 
      { 
       DBConnection.close(statePreparedStmt,stateResultSet); 
      } 
      catch(Exception e) 
      { 
       logger.error(e); 
      } 

      try 
      { 
       DBConnection.close(dbConnection); 
      } 
      catch(Exception e) 
      { 
       logger.error(e); 
      } 

     } 
     return response; 
    } 
} 

您能否讓我知道如何解決問題?

+1

您確定您實際上正在監視活動連接(如'SHOW FULL PROCESSLIST'),而不僅僅是一個增量計數器,如[這裏]所述(https://dev.mysql.com/doc/refman/5.7/) EN /服務器狀態,variables.html#statvar_Connections)? – Cascader

+0

@Cascader或者也許更簡單https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html#statvar_Threads_connected –

回答

1

您正在使用連接池。您的池可以創建多達25個連接。關閉連接時,它並未真正關閉,但已釋放到池中。

+0

我寫了一個簡單的JDBC程序,它獲取一個連接並在finaly block中關閉它(根本不涉及連接池和我的代碼),但是我仍然看到連接沒有被關閉。 – Pawan

+0

打開連接代價昂貴,您應該檢查數據庫驅動程序,也許驅動程序未關閉連接以再次使用它。 – ydemartino

相關問題