在野蠻web應用程序中使用NamedParameterJdbcTemplate
檢索記錄列表中的後臺作業時,數據庫連接泄漏阻止作業在一段時間後工作,CannotGetJdbcConnectionException
。春季JDBC模板:queryforlist不關閉數據庫連接?
在監視了一部分sql請求之後,這裏是我發現的。對於這個簡單的代碼:
String sqlselectreq =
"SELECT * " +
"FROM systemedpdfdb.documents " +
"WHERE state=-3 AND closing_date IS null";
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new
NamedParameterJdbcTemplate(this.getDataSource());
Map<String, Object> namedParameters = new HashMap<String, Object>();
// namedParameters.put("param_name_here", param_value_here);
List<Map<String, Object>> queryres =
namedParameterJdbcTemplate.queryForList(sqlselectreq, namedParameters);
if (!(queryres == null || queryres.isEmpty())) {
for (Map<String, Object> queryrec : queryres) {
....
}
} else {
return new Result(this, Result.CodeCategory.NOERROR,
Result.Type.TEXT, "NOTHING TO DO".getBytes(), ident);
}
我有以下的痕跡在這裏我們可以看到一個無用的最後的getConnection無收。
2017-05-11 15:37:00,031 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [DataSource] getConnection()
2017-05-11 15:37:00,281 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] prepareStatement(SELECT * FROM systemedpdfdb.documents WHERE state=-3 and closing_date is null)
2017-05-11 15:37:00,297 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] executeQuery()
2017-05-11 15:37:00,312 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [ResultSet] next()
2017-05-11 15:37:00,312 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [ResultSet] close()
2017-05-11 15:37:00,312 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] isClosed()
2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] close()
2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] isClosed()
2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] close()
2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [DataSource] getConnection()
2017-05-11 15:37:00,328 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] getMetaData()
這是正常的嗎?或者我濫用Spring Jdbc模板?提前
感謝
大衛L.
添加一些記錄到'的queryForList後調用,這樣我們就可以確定額外的'getConnection'是從那裏創建的。 – jingx