我正在使用Java應用程序前端來連接MySQL 5.6服務器上的數據庫並與其進行交互。使用來自MySQL的JDBC連接器當調用con.close()時,我遇到了服務器連接不能關閉的問題。當應用程序關閉時,連接全部被刪除。以下是用於查詢的dao類。Java MySQL連接未關閉
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;然後我得到的輸出:
沒有連接
獲取連接並工作
連接是最後才
終止所以連接在這一點上順利結束,但我覺得這不是正確的做法。
+1使用con.isClosed()檢查後,請參閱http://docs.oracle.com/javase/6/docs/api/java/sql/Connection.html#isClosed() – bstempi
,它工作正常,但是當觀察與監控服務器的MySQL Workbench的連接,我仍然可以看到連接仍然存在。 – Danny
就像下面的人說的那樣,這可能會在服務器端聚合 - 這種連接可能會等待重新打開或等待另一個連接取代它。 – Josh