java.sql.SQLException:已关闭 [英] java.sql.SQLException: Already closed

查看:311
本文介绍了java.sql.SQLException:已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Web应用程序在带有MySQL后端的tomcat上在生产环境中运行.一段时间以来一切都很好,然后突然我们开始收到此异常java.sql.SQLException: Already closed.

We have a webapp running in production on tomcat with a MySQL back-end. All was fine for sometime, then suddenly we started getting this exception java.sql.SQLException: Already closed.

整个堆栈跟踪为:

DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Fetching JDBC Connection from DataSource
DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Returning JDBC Connection to DataSource
DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Could not close JDBC Connection    
java.sql.SQLException: Already closed.
    at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:114)
    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:191)
    at org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:333)
    at org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection(DataSourceUtils.java:294)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:405)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:428)
    at com.nokia.analytics.aws.aggregate.service.importer.DBInsert.truncateTable(DBInsert.java:135)
    at com.blah.analytics.aggregate.service.importer.AggregateCollector.pullAndInsert(AggregateCollector.java:85)
    at com.blah.analytics.aggregate.service.importer.AggregateCollector.call(AggregateCollector.java:96)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:679)

我们正在使用org.apache.commons.dbcp.BasicDataSource作为我们的数据源.我搜索了很多,但无济于事. 它并不总是发生,因此很难复制. db连接池似乎有问题.建议在某处设置否定参数.目前,我们不会更改这些参数(所有参数均具有默认值).

We are using org.apache.commons.dbcp.BasicDataSource as our datasource. I searched quite a bit but to no avail. It doesn't occur always and hence is very hard to reproduce. It seems a problem with db connection pooling. Somewhere it was suggested to set this param as negative. Currently we are not changing of those parameters (all have default vals).

我们应该采取什么方法来避免这种情况?

What approach should we follow to avoid it?

相关代码在(DBInsert.java)中

The relevant code is in (DBInsert.java)

133:String sql = "DELETE FROM "+tableName;

134:logger.debug(sql);

135:this.jdbcTemplate.execute(sql);

(133-135是例外中指定的行号)

(133-135 are line nos. which are specified in the exception)

我的数据源配置:

<bean id="bisToolDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="${url}/blah_db?verifyServerCertificate=false&amp;useSSL=true&amp;requireSSL=true" />
        <property name="username" value="${uname}" />
        <property name="password" value="${passwd}" />
    </bean>

推荐答案

此问题的原因是长时间未使用连接,将testOnBorrowvalidationQuery属性添加到数据源配置中,然后您的应用程序将工作正常.
祝你好运:)

The cause of this problem is connection isn't used in a long time, add testOnBorrow and validationQuery property to your datasource configuration then your application will work fine.
Good luck:)

这篇关于java.sql.SQLException:已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆