Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: Connection has already been closed

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 a way fix this issue or help to find out the root cause.

like image 475
Mukesh Kumar Avatar asked Aug 11 '15 12:08

Mukesh Kumar


1 Answers

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.

like image 135
puneet goyal Avatar answered Nov 19 '22 18:11

puneet goyal