Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringJDBC模板:queryforlist不';t关闭数据库连接?_Java_Spring_Jdbc_Jboss_Wildfly 10 - Fatal编程技术网

Java SpringJDBC模板:queryforlist不';t关闭数据库连接?

Java SpringJDBC模板:queryforlist不';t关闭数据库连接?,java,spring,jdbc,jboss,wildfly-10,Java,Spring,Jdbc,Jboss,Wildfly 10,在wildfly web应用程序中有后台作业,其中NamedParameterJdbcTemplate用于检索记录列表,我有一个数据库连接泄漏,阻止作业在一段时间后工作,无法获取JDBCConnectionException 在监视了部分sql请求之后,我发现了以下内容。对于这段简单的代码: String sqlselectreq = "SELECT * " + "FROM systemedpdfdb.documents " + "WHERE state=-3 AND

在wildfly web应用程序中有后台作业,其中
NamedParameterJdbcTemplate
用于检索记录列表,我有一个数据库连接泄漏,阻止作业在一段时间后工作,
无法获取JDBCConnectionException

在监视了部分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);

}
这正常吗?还是我误用了SpringJDBC模板

提前谢谢


David L.

实际的数据库连接由数据源实现包装。您使用的是什么数据源实现

在web应用程序中,您应该(始终?)使用提供已建立连接池(也称为连接池)的数据源。有几种优秀的数据库连接池实现可用。例如,DBCP、c3p0和Heroku


另外,如果您使用的是Spring,只需将数据源注入JDBCTemplatebean,然后将JdbcTemplate注入DAO类,而不是动态创建一个新的NamedParameterJdbcTemplate实例。

谢谢您的帮助。。。。最后一个无用的未关闭连接是由错误的日志记录系统造成的,因为下面的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() 

queryForList
调用之后添加一些日志记录,这样我们就可以确定额外的
getConnection
是从那里创建的。
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()