2013-10-09 136 views
1

我正在使用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;然后我得到的输出:

没有连接

获取连接并工作

连接是最后才

终止所以连接在这一点上顺利结束,但我觉得这不是正确的做法。

回答

2

只会是空我想你想con.isClosed()而不是con==null为您检查。仅仅因为您关闭了Connection并不意味着您对该对象本身的引用将是null

+0

+1使用con.isClosed()检查后,请参阅http://docs.oracle.com/javase/6/docs/api/java/sql/Connection.html#isClosed() – bstempi

+0

,它工作正常,但是当观察与监控服务器的MySQL Workbench的连接,我仍然可以看到连接仍然存在。 – Danny

+0

就像下面的人说的那样,这可能会在服务器端聚合 - 这种连接可能会等待重新打开或等待另一个连接取代它。 – Josh

1

您的getDBConnection()方法正在返回一个关闭的连接。所以你将永远无法使用该连接。不知道在getDBConnection()方法中关闭连接的逻辑是什么。

您应该有一个不同的方法来关闭连接。

+0

该方法实际上并不返回关闭的连接。它的唯一设置是在try块中失败时关闭。 – Danny

2

关闭连接与对象con无效无关。您的程序可能正在运行,并且您的连接已关闭。你的对象con,如果你强迫它

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

调用Connection对象上的close()方法并不意味着Connection对象将是nullclose()方法的执行的结束。

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