简体   繁体   中英

Java MySQL Connection not closing

I am using a Java application front end to connect and interact with a database on a MySQL 5.6 server. Using the JDBC connector from MySQL I am having issues with connections made to the server not closing when con.close() is called. The connections are all dropped when the application is closed though. The following is the dao class used to make queries.

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

The next bit of code is a method to verify the user of the application with the database. There are some System.out.println() statements to track the con variable through the process.

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

The output from running that method is:

there is not a connection

get connection did work

connection was not terminated before finally

trying again...

there is a connection still

If I change the con.close(); to con = null; then I get the output:

there is not a connection

get connection did work

connection was terminated before finally

So the connection is successfully terminating at that point, but I feel like this is not the proper way to do it.

Closing the connection has nothing to do with the object con being null. Your program is probably working and you had your connection closed. Your object con will only be null if you force it

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

I think you want con.isClosed() rather than con==null for your check. Just because you've closed a Connection doesn't mean your reference to the object itself will be null .

Calling the close() method on a Connection object doesn't mean that the Connection object will be null at the end of execution of the close() method.

If you want to test if your connection has been closed successfully then you should call the isClosed() method.

Your getDBConnection() method is returning a closed connection. So you will never be able to use that connection. Not sure what is the logic of closing the connection in getDBConnection() method.

You should have a different method to close the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM