我已经实现了这个 JDBC 代码,并且在周末我们遇到了一个问题,即连接可能会泄漏,并且数据库功能停止工作。虽然我不确定这是否是问题所在,但我想通过 Stack Overflow 社区传递它,看看是否有任何代码确实容易泄漏。我相信我已经通过 Try with resources 缩小了所有范围,但也许我错过了一些东西。所有帮助表示赞赏。
public ArrayList<LinkedHashMap> runReportQuery(final DataSource dataSource, final String asms, final String country) {
final ArrayList<LinkedHashMap> reportList = new ArrayList<>();
this.executeQuery(dataSource, "select * from table where id =5", "customerReportType", true, reportList);
return reportList;
}
private void executeQuery(final DataSource queryDataSource, final String sql, final String reportType,
final Boolean isMarketSpecific, final ArrayList<LinkedHashMap> reportList ){
try(Connection conn = queryDataSource.getConnection();
PreparedStatement ps = this.createPreparedStatement(conn, isMarketSpecific, sql);
ResultSet rs = ps.executeQuery();
) {
while(rs.next()){
final LinkedHashMap<String, String> reportMap = new LinkedHashMap<>();
//Creating report string that adds columns and respective values to report - This way we do not need to deal with DTO object creation logic
String reportString= "";
//Iterate through each column, add column and respective data.
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++)
reportString = reportString + rs.getMetaData().getColumnLabel(i) + ": " + rs.getString(i) +"!$";
reportMap.put("reportItem", reportType + "!$"+ reportString);
reportList.add(reportMap);
}
}
catch (final SQLException e){
LOG.info("SqlException Occured", e);
e.printStackTrace();
throw new RuntimeException(e);
}
}
private PreparedStatement createPreparedStatement(final Connection con, final boolean isMarketSpecific, final String sql) throws SQLException {
final PreparedStatement statement = con.prepareStatement(sql);
if(isMarketSpecific)
{
statement.setString(1, this.asms);
statement.setString(2, this.country);
}
return statement;
}