java.sql.SQLException: 连接已经关闭

java.sql.SQLException: Connection has already been closed

我们收到 java.sql.SQLException:连接已关闭。执行事务时间歇性异常。我们正在使用 tomcat 7.X 下面是配置。

<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>

可能是我们缺少某些配置或 属性 这里导致了问题。

请提出解决此问题的方法或帮助找出根本原因。

以下配置对我有用

<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" />

这里的键值为connectionTimeout。 您当前使用的工厂有一个默认超时,超时后它会强制会话关闭。

上面的连接超时值对我有用,对于您的应用程序场景,您必须进行一些试验才能获得正确的值。

添加以下值:

removeAbandonedTimeout="600"