首页 文章

Java MySQL Connection没有关闭

提问于
浏览
1

我正在使用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;
}

运行该方法的输出是:

没有连接获取连接确实工作连接没有终止,最后再次尝试...还有一个连接仍然如果我改变了con.close(); con = null;然后我得到输出:没有连接获得连接确实工作连接终止之前终止

所以连接在那时成功终止,但我觉得这不是正确的方法 .

4 回答

  • 1

    我认为你想要 con.isClosed() 而不是 con==null . 仅仅因为你关闭 Connection 并不意味着你对对象本身的引用将是 null .

  • 2

    关闭连接与对象 con 无关 . 您的程序可能正常工作,您的连接已关闭 . 如果你强制它,你的对象 con 将只为null

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

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

    如果要测试连接是否已成功关闭,则应调用 isClosed() 方法 .

  • 2

    您的 getDBConnection() 方法正在返回已关闭的连接 . 所以你将永远无法使用该连接 . 不确定在 getDBConnection() 方法中关闭连接的逻辑是什么 .

    您应该使用其他方法来关闭连接 .

相关问题