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

次のコードは、アプリケーションのユーザーをデータベースで検証する方法です。プロセスを通じて con 変数を追跡する System.out.println() ステートメントがいくつかあります。

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(); を変更すると コン=ヌルに。次に、出力を取得します。

接続がありません

接続を取得する

最終的に接続が終了しました

その時点で接続は正常に終了していますが、これは適切な方法ではないように感じます。

4

4 に答える 4

2

con接続を閉じることは、オブジェクトが nullであることとは関係ありません。プログラムはおそらく動作しており、接続が閉じられています。オブジェクトconは、強制した場合にのみ null になります

...
if(con != null){
    System.out.println("connection was not terminated before finally");
    System.out.println("trying again...");
    con.close();
    con = null;
}
....
于 2013-10-09T14:27:19.440 に答える
2

close()オブジェクトでメソッドを呼び出しても、オブジェクトがメソッドの実行の最後にあるConnectionというわけではありません。Connectionnullclose()

接続が正常に閉じられたかどうかをテストする場合は、isClosed()メソッドを呼び出す必要があります。

于 2013-10-09T14:31:50.213 に答える
2

con.isClosed()というよりcon==nullは、ご確認をお願いしたいと思います。を閉じたからとConnectionいって、オブジェクト自体への参照が になるわけではありませんnull

于 2013-10-09T14:31:24.487 に答える