【问题标题】:Java MySQL Connection not closingJava MySQL连接未关闭
【发布时间】:2013-10-16 23:10:26
【问题描述】:

我正在使用 Java 应用程序前端连接 MySQL 5.6 服务器上的数据库并与之交互。使用 MySQL 中的 JDBC 连接器时,当调用 con.close() 时,与服务器建立的连接没有关闭。但是,当应用程序关闭时,连接都将被删除。以下是用于查询的dao类。

package binaparts.dao;

import java.sql.*;

import org.json.JSONArray;
import org.json.JSONObject;

import binaparts.properties.*;
import binaparts.util.ToJSON;

public class DBConnect {

protected Statement st = null;
protected ResultSet rs = null;
protected Connection con = null;    
protected PreparedStatement pst = null;
private String configFilePath = "config.properties";
public DBConnect(){
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
}
private Connection getDBConnection() throws Exception{

    ConfigurationManager configProps = new ConfigurationManager(configFilePath);
    String host = configProps.getProperty("host");
    String port = configProps.getProperty("port");
    String database = configProps.getProperty("database");
    String user = configProps.getProperty("user");
    String password = configProps.getProperty("password");

    try{
        con = DriverManager.getConnection("jdbc:mysql" + "://" + host + ":" + port + "/" + database, user, password);
    }catch(SQLException SQLex){
        con = null;
    }catch(Exception ex){
        ex.printStackTrace();
        con = null;
    }finally{
        try{rs.close();} catch(Exception ex) { /*ignore*/}
        try{st.close();} catch(Exception ex) { /*ignore*/}
        try{pst.close();} catch(Exception ex) { /*ignore*/}
    }
return con;
}

下一段代码是使用数据库验证应用程序用户的方法。有一些 System.out.println() 语句可以在整个过程中跟踪 con 变量。

public boolean verifyUser() throws Exception {

    ConfigurationManager config = new ConfigurationManager(configFilePath);
    String username = config.getProperty("appUser");
    String password = config.getProperty("appPassword");
    boolean userCheck = false;
    try{
        if(con == null){
        System.out.println("there is not a connection");
        }
        if(con != null){
            System.out.println("there is a connection");
        }
        con = getDBConnection();
        if(con == null){
            System.out.println("get connection did not work");
        }
        if(con != null){
            System.out.println("get connection did work");
        }
        JSONObject temp = queryReturnUser(username).getJSONObject(0);
        con.close();
        String un = null;
        try{
            un = temp.get("username").toString();
        }catch(Exception ex){un = " ";}

        String pw = null;
        try{
            pw = temp.get("password").toString();
        }catch(Exception ex){pw = " ";}

        if(username.equals(un)){
            if(password.equals(pw)){
                userCheck = true;
            }
        }
    }catch(Exception ex){/*ignore*/}
    finally{
        try{
            if(con == null){
                System.out.println("connection was terminated before finally");
            }
            if(con != null){
                System.out.println("connection was not terminated before finally");
                System.out.println("trying again...");
                con.close();
            }
            if(con != null){
                System.out.println("there is a connection still");
            }
        }catch(Exception ex){ex.printStackTrace();}
    }
return userCheck;
}

运行该方法的输出是:

没有连接

获取连接确实有效

在 finally 之前没有终止连接

再试一次……

还有连接

如果我改变 con.close();到 con = null;然后我得到输出:

没有连接

获取连接确实有效

连接在 finally 之前终止

所以此时连接已成功终止,但我觉得这不是正确的方法。

【问题讨论】:

    标签: java mysql jdbc connection


    【解决方案1】:

    您的getDBConnection() 方法正在返回一个关闭的连接。因此,您将永远无法使用该连接。不确定getDBConnection() 方法中关闭连接的逻辑是什么。

    您应该有不同的方法来关闭连接。

    【讨论】:

    • 该方法实际上并没有返回一个关闭的连接。如果在 try 块期间出现故障,它只会设置为关闭。
    【解决方案2】:

    关闭连接与对象con 为空无关。您的程序可能正在运行,并且您的连接已关闭。您的对象 con 只有在您强制时才会为空

    ...
    if(con != null){
        System.out.println("connection was not terminated before finally");
        System.out.println("trying again...");
        con.close();
        con = null;
    }
    ....
    

    【讨论】:

      【解决方案3】:

      我认为您需要 con.isClosed() 而不是 con==null 进行检查。仅仅因为您关闭了 Connection 并不意味着您对对象本身的引用将是 null

      【讨论】:

      • 用con.isClosed()检查后,工作正常,但是用MySQL Workbench监控服务器的连接时,我仍然可以看到连接还在。
      • 就像下面有人说的那样,这可能是在服务器端进行池化 - 该连接可能会等待重新打开或等待另一个连接取代它。
      【解决方案4】:

      Connection 对象上调用close() 方法并不意味着Connection 对象在close() 方法执行结束时将是null

      如果您想测试您的连接是否已成功关闭,那么您应该调用isClosed() 方法。

      【讨论】:

        猜你喜欢
        • 2014-12-18
        • 2014-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2021-04-14
        • 2020-09-16
        相关资源
        最近更新 更多