2017-05-11 115 views
0

在野蠻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.

+0

添加一些記錄到'的queryForList後調用,這樣我們就可以確定額外的'getConnection'是從那裏創建的。 – jingx

回答

0

感謝您的幫助....最後沒用未關閉的連接是由一個有錯誤的記錄系統造成如下面的JBoss的日誌讓他猜...

2017-05-11 18:12:00,024 INFO [eu.systemed.commands.workflow.CloseCancelledDocumentCommand] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) before execution of the query:SELECT * FROM systemedpdfdb.documents WHERE state=-3 and closing_date is null 
2017-05-11 18:12:00,039 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [DataSource] getConnection() 
2017-05-11 18:12:00,273 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 18:12:00,289 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] executeQuery() 
2017-05-11 18:12:00,305 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [ResultSet] next() 
2017-05-11 18:12:00,305 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [ResultSet] close() 
2017-05-11 18:12:00,305 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] isClosed() 
2017-05-11 18:12:00,320 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [PreparedStatement] close() 
2017-05-11 18:12:00,320 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] isClosed() 
2017-05-11 18:12:00,320 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] close() 
2017-05-11 18:12:00,320 INFO [eu.systemed.commands.workflow.CloseCancelledDocumentCommand] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) after execution of the query:SELECT * FROM systemedpdfdb.documents WHERE state=-3 and closing_date is null 
2017-05-11 18:12:00,320 INFO [eu.systemed.commands.workflow.CloseCancelledDocumentCommand] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) empty result for the query:SELECT * FROM systemedpdfdb.documents WHERE state=-3 and closing_date is null 
2017-05-11 18:12:00,320 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [DataSource] getConnection() 
2017-05-11 18:12:00,320 DEBUG [jboss.jdbc.spy] (org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1) java:jboss/datasources/SystemedPdfdataSource4jobs [Connection] getMetaData() 
0

實際的數據庫連接是由您的DataSource實現包裹。您使用的DataSource實現是什麼?

在Web應用程序中,您應該(總是使用)提供已建立連接池(又名連接池)的DataSource。有幾個優秀的數據庫連接池實現可用。例如,DBCP,c3p0和Heroku。另外,如果您使用的是Spring,只需將您的數據源注入JdbcTemplate bean,然後將JdbcTemplate注入到您的DAO類中,而不是即時創建新的NamedParameterJdbcTemplate實例。