数据库连接未关闭

时间:2013-06-24 14:33:03

标签: java mysql

我的数据库连接无法关闭的原因是什么? 该错误表示当我执行多个查询时连接太多

先谢谢,这是代码:

   try {
        st = DBConnector.getConnection().prepareStatement(sqlQuery);
        st.setString(1, userID);
        result = st.executeQuery();
        checker = !result.first();

    }catch(Exception e){
        e.printStackTrace();
    }
    finally
    {
        try {
            DBConnector.getConnection().close();
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

顺便说一句,这是我的DBConnector类

public class DBConnector {
    private static Connection;
    private static String host;
    private static String username;
    private static String password ;


    public static void setUsername(String newusername){
        username = newusername;
    }
    public static void setPassword(String newpassword){
        password = newpassword;
    }
    public static void setHost(String newHost){
        host = newHost;
    }
    public static void setConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection(host, username , password);

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

    }

    public static String getUsername(){
        return username;
    }
    public static String getPassword(){
        return password;
    }
    public static String getHost(){
        return host;
    }
    public static Connection getConnection(){
        setConnection();
        return conn;
    }

    public static void disconnect(){
        conn = null;
    }

}

2 个答案:

答案 0 :(得分:0)

如果我正确阅读了您的课程,则会创建一个新的连接,该连接会立即在finally块中关闭,而不会在执行DBConnector.getConnection().close()时关闭原始连接。

将您的代码更改为:

 Connection connection = DBConnector.getConnection();
 try {        
     st = connection .prepareStatement(sqlQuery);
     // more code
  } catch (...) {
     // more code
  finally
    {
        try {
            connection.close(); // 
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

更好的是使用try-with-resources(Java 7):

 try (Connection connection = DBConnector.getConnection()) {        
     st = connection .prepareStatement(sqlQuery);
     // more code
 } catch (...) {
     // more code
 }

答案 1 :(得分:0)

这是您正确打开和关闭连接的方式。

/**
 * Creates a connection to database if there isn't any established.
 * 
 * @return {@link Connection} to database.
 */
protected Connection getConnection() throws ServiceException{
    try{
        if(this.conn == null || this.conn.isClosed()){
            // Here you have to create your connection
            // this.conn = ...
        }
        if(conn==null){
            // Retry to connect in case that the connection is still null!
            Thread.sleep(250);
            getConnection();
        }
    }catch(SQLException e ){
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return conn;
}


/**
 * Closes the {@link Connection} to database.
 */
protected void closeConnection(){
    try{
        if(this.conn != null && !this.conn.isClosed()){
            this.conn.close();
        }

    }catch(SQLException e){
        e.printStackTrace();
    }
    finally{
        this.conn = null;
    }
}
相关问题