【问题标题】:java.sql.SQLException: Connection has already been closedjava.sql.SQLException:连接已关闭
【发布时间】:2015-11-03 16:27:22
【问题描述】:

我们收到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>

可能我们在此处缺少某些导致问题的配置或属性。

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

【问题讨论】:

  • 这种情况发生在批量数据还是少量数据中
  • 当我们处理一些批量数据时会发生这种情况。

标签: java postgresql tomcat jndi


【解决方案1】:

以下配置对我有用

<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。 您当前使用的工厂有一个默认超时,之后它会强制会话关闭。

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

【讨论】:

    【解决方案2】:

    添加以下值:

    removeAbandonedTimeout="600"
    

    【讨论】:

    猜你喜欢
    • 2014-09-19
    • 2016-07-01
    • 2011-08-02
    • 2016-06-04
    • 2012-10-26
    • 1970-01-01
    • 2013-04-26
    • 2017-07-18
    相关资源
    最近更新 更多