java.sql.SQLException: Connection has already been closed

偶尔善良 提交于 2019-12-21 02:44:09

问题


We are getting java.sql.SQLException: Connection has already been closed. exception intermittently while performing a transaction. We are using tomcat 7.X and below is the configuration.

<Context docBase="ROOT" reloadable="true" antiJARLocking="true">
        <Resource
                name="jdbc/DS"
                auth="Container"
                type="javax.sql.DataSource"
                driverClassName="org.postgresql.Driver"
                url="jdbc:postgresql://XXXXXXX"
                factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
                username="XXXXXX"
                password="XXXXXX"
                maxActive="20"
                maxIdle="3"
                minIdle="3"
                maxWait="10000"
                removeAbandoned="true"/>
</Context>

Probably we are missing some configuration or property here that is causing the issue.

Please suggest any other library to fix this issue or help to find out the root cause.

Thanks in advance.


回答1:


Following configuration worked for me

<Context context="ROOT" debug="0" reloadable="false" useHttpOnly="true" cacheMaxSize="40960" cacheTTL="60000" cachingAllowed="true" antiJARLocking="true">
    <Resource name="XYZ" auth="Container"
            description="Exchange DB Connection"
            dataSourceClassName="org.postgresql.ds.PGSimpleDataSource"
            dataSource.serverName="XXXXX"
            dataSource.databaseName="XXXX"
            dataSource.portNumber="XXXX"
            dataSource.user="xyz"
            dataSource.password="xyz"
            maximumPoolSize="20"
            minimumIdle="5"
            connectionTimeout="300000"
            factory="com.zaxxer.hikari.HikariJNDIFactory"
            registerMbeans="true"
            type="javax.sql.DataSource" />

The key value here is connectionTimeout. The factory which you are currently using has a default timeout, after that it forces session to close.

The connection timeout value above worked for me , for your application scenarios you'll have to experiment a bit to get the right value.




回答2:


add below value:

removeAbandonedTimeout="600"


来源:https://stackoverflow.com/questions/31941889/java-sql-sqlexception-connection-has-already-been-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!