最近,我不得不测量我们软件的一些SQL请求所花费的时间。为此,我决定采用天真的方法并使用对System.nanoTime()的调用来环绕查询。 在这样做时,我发现了一个类(SqlSuggest),它包含4种非常相似的方法,包含4个非常相似的方法。我认为重构和重新组合公共部分是个好主意。
这造成连接泄漏,连接不再关闭。我回滚了重构,但我还是想了解我做错了什么。
SqlSuggest类的第一个版本有4个方法getSuggestListByItems,getDisplayValueByItems,getSuggestListByListID和getDisplayValueByListeID。这些方法中的每一个都通过另一个类打开和关闭Connection,Statement和ResultSet(在finally块中):DBAccess。
该类的第二个版本具有相同的4个方法,除了不打开和关闭Connection,它们根据是否需要一个结果或列表来调用2个方法中的1个。
这两个方法(executeQueryGetString和executeQueryGetListOfStringArray)分别声明Connection,Statement和ResultSet,然后调用一个方法:executeQuery,其中Connection打开,创建Statement并返回ResultSet。
第二层方法然后从ResultSet中提取数据并关闭所有内容。
我猜我错误地认为在方法中声明Connection,Statement和ResultSet会允许我从同一个方法中关闭它们。
这是旧类(简化):
public class SqlSuggest {
private final static Logger LOGGER = LogManager.getLogger();
public List<String[]> getSuggestListByItems(String codeSite) {
List<String[]> suggestList = new ArrayList<>();
String query = "SELECT iDcode, designation FROM Mytable WHERE codeSite = " + codeSite; // Simplified query building
DBAccess accesBD = new DBAccess();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = accesBD.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
suggestList.add(new String[]{rs.getString(1), rs.getString(2)});
}
} catch (NamingException | SQLException ex) {
LOGGER.error("", ex);
} finally {
accesBD.closeResultSet(rs);
accesBD.closeStatement(stmt);
accesBD.closeConnection(conn);
}
return suggestList;
}
public String getDisplayValueByItems(String table, String codeSite) {
String displayValue = null;
String query = "SELECT iDcode, designation FROM " + table + " WHERE codeSite = " + codeSite; // Different query building
DBAccess accesBD = new DBAccess();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = accesBD.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if (rs.next()) {
displayValue = rs.getString(1);
}
} catch (NamingException | SQLException ex) {
LOGGER.error("", ex);
} finally {
accesBD.closeResultSet(rs);
accesBD.closeStatement(stmt);
accesBD.closeConnection(conn);
}
return displayValue;
}
public List<String[]> getSuggestListByListID(String listeID, String table, String codeSite) {
List<String[]> suggestList = new ArrayList<>();
String query = "SELECT iDcode, designation FROM " + table + " WHERE codeSite = " + codeSite; // Different query building involving listID
DBAccess accesBD = new DBAccess();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = accesBD.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
suggestList.add(new String[]{rs.getString(1), rs.getString(2)});
}
} catch (NamingException | SQLException ex) {
LOGGER.error("", ex);
} finally {
accesBD.closeResultSet(rs);
accesBD.closeStatement(stmt);
accesBD.closeConnection(conn);
}
return suggestList;
}
public String getDisplayValueByListeID(String listeID, String table, String codeSite) {
String displayValue = null;
String query = "SELECT iDcode, designation FROM " + table + " WHERE codeSite = " + codeSite; // Different query building involving listID
DBAccess accesBD = new DBAccess();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = accesBD.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if (rs.next()) {
displayValue = rs.getString(2);
}
} catch (NamingException | SQLException ex) {
LOGGER.error("", ex);
} finally {
accesBD.closeResultSet(rs);
accesBD.closeStatement(stmt);
accesBD.closeConnection(conn);
}
return displayValue;
}
}
这是新类(具有泄漏的类):
public class SqlSuggest {
private static final Logger LOGGER = LogManager.getLogger();
public List<String[]> getSuggestListByItems(String codeSite) {
List<String[]> suggestList;
String query = "SELECT iDcode, designation FROM Mytable WHERE codeSite = " + codeSite; // Simplified query building
suggestList = executeQueryGetListOfStringArray(query);
return suggestList;
}
public String getDisplayValueByItems(String table, String codeSite) {
String displayValue = null;
String query = "SELECT iDcode, designation FROM " + table + " WHERE codeSite = " + codeSite; // Different query building
int columnToFetch = 1;
displayValue = executeQueryGetString(query, columnToFetch);
return displayValue;
}
public List<String[]> getSuggestListByListID(String listeID, String table, String codeSite) {
List<String[]> suggestList = new ArrayList<>();
String query = "SELECT iDcode, designation FROM " + table + " WHERE codeSite = " + codeSite; // Different query building involving listID
suggestList = executeQueryGetListOfStringArray(query);
return suggestList;
}
public String getDisplayValueByListeID(String listeID, String table, String codeSite) {
String displayValue = null;
String query = "SELECT iDcode, designation FROM " + table + " WHERE codeSite = " + codeSite; // Different query building involving listID
int columnToFetch = 2;
displayValue = executeQueryGetString(query, columnToFetch);
return displayValue;
}
private String executeQueryGetString(String query, int columnToFetch){
String result = null;
Statement stmt = null;
Connection conn = null;
ResultSet rs = executeQuery(query, stmt, conn);
try{
if (rs.next()) {
result = rs.getString(columnToFetch);
}
} catch (SQLException ex) {
LOGGER.error("", ex);
} finally {
DBAccess accesBD = new DBAccess();
accesBD.closeResultSet(rs);
accesBD.closeStatement(stmt);
accesBD.closeConnection(conn);
}
return result;
}
private List<String[]> executeQueryGetListOfStringArray(String query){
List<String[]> result = new ArrayList<>();
Statement stmt = null;
Connection conn = null;
ResultSet rs = executeQuery(query, stmt, conn);
try{
while (rs.next()) {
result.add(new String[]{rs.getString(1), rs.getString(2)});
}
} catch (SQLException ex) {
LOGGER.error("", ex);
} finally {
DBAccess accesBD = new DBAccess();
accesBD.closeResultSet(rs);
accesBD.closeStatement(stmt);
accesBD.closeConnection(conn);
}
return result;
}
private ResultSet executeQuery(String query, Statement stmt, Connection conn){
DBAccess accesBD = new DBAccess();
ResultSet rs = null;
try {
conn = accesBD.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
} catch (NamingException | SQLException ex) {
LOGGER.error("", ex);
}
return rs;
}
}
这是实际打开和关闭连接的类(它没有以任何方式改变):
public class DBAccess {
private final static Logger LOGGER = LogManager.getLogger();
public void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
LOGGER.error("", e);
}
}
}
public void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
LOGGER.error("", e);
}
}
}
public void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
LOGGER.error("", e);
}
}
}
public Connection getConnection() throws SQLException, NamingException {
Connection cnx = null;
PropertiesManager manager = PropertiesDelegate.getPropertiesManager();
String jndi = manager.getProperty("datasource.name", "QuartisWeb-PU");
Context ctx = null;
DataSource dataSource = null;
try {
ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup(jndi);
} catch (NamingException ex) {
try {
dataSource = (DataSource) ctx.lookup("java:comp/env/" + jndi);
} catch (NamingException ex1) {
LOGGER.error("", ex1);
}
}
if (dataSource != null) {
cnx = dataSource.getConnection();
}
return cnx;
}
}
请忽略这些参数未在查询中正确设置的事实,我知道应该这样做。
答案 0 :(得分:3)
当您从executeQueryGetString()
拨打executeQuery()
时,conn
和stmt
的值将在executeQuery()
内更改,但仍会null
返回时在executeQueryGetString()
内。因此,在finally
的{{1}},对executeQueryGetString()
的调用实际上是accesBD.closeConnection(conn)