Search code examples
javamysqlc3p0

java.sql.SQLException: No operations allowed after connection closed . DB: mysql


Exception : java.sql.SQLException: No operations allowed after connection closed.

I got this strange exception when I tried to run my application. I am using local variable for connection object and after execution of some statements this exception is raising. I am using c3p0 connection pool and MySQL database.


Solution

  • It is difficult to diagnose this without seeing your code, but I once had an similar problem due to the fact that mysql closes the connections after a while; so make sure that the configuration of your datasource has a validation query, and testOnBorrow and testWhileIdle are both true.

    Here is an example:

        <Resource auth="Container" type="javax.sql.DataSource" name="jdbc/mydb"
                driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://myserver/mydb"
                maxActive="10"
                maxIdle="5"
                validationQuery="SELECT 1"
                testOnBorrow="true"
                testWhileIdle="true"
                timeBetweenEvictionRunsMillis="10000"
                minEvictableIdleTimeMillis="60000"
                username="???" password="???"/>