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