-1

我的数据库连接不会关闭的原因是什么?错误说当我执行多个查询时连接太多

在此先感谢,这是代码:

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

}
4

2 回答 2

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
 }
于 2013-06-24T15:03:43.213 回答
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;
    }
}
于 2013-06-24T15:53:04.220 回答