Cause: java.sql.SQLException: Connection is closed

My Sprin Boot Application got exception which is “Cause: java.sql.SQLException: Connection is closed”,I think this is the same problem to cause that but I can not figure out why,detail exceptions like bellow:

#####exception 1
Caused by: org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: java.sql.SQLException: Connection is closed

The error may exist in org/flowable/db/mapping/entity/Execution.xml

The error may involve org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl.selectProcessInstanceByQueryCriteria

The error occurred while executing a query

SQL: select distinct RES.* , P.KEY_ as ProcessDefinitionKey, P.ID_ as ProcessDefinitionId, P.NAME_ as ProcessDefinitionName, P.VERSION_ as ProcessDefinitionVersion, P.DEPLOYMENT_ID_ as DeploymentId from ACT_RU_EXECUTION RES inner join ACT_RE_PROCDEF P on RES.PROC_DEF_ID_ = P.ID_ WHERE RES.PARENT_ID_ is null and RES.ID_ = ? and RES.PROC_INST_ID_ = ? order by RES.ID_ asc

Cause: java.sql.SQLException: Connection is closed

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.flowable.common.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:271) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.db.DbSqlSession.queryWithRawParameter(DbSqlSession.java:250) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:165) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.persistence.entity.data.impl.MybatisExecutionDataManager.findProcessInstanceByQueryCriteria(MybatisExecutionDataManager.java:252) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.persistence.entity.ExecutionEntityManagerImpl.findProcessInstanceByQueryCriteria(ExecutionEntityManagerImpl.java:146) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.ProcessInstanceQueryImpl.executeList(ProcessInstanceQueryImpl.java:824) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.query.AbstractQuery.executeSingleResult(AbstractQuery.java:171) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.query.AbstractQuery.execute(AbstractQuery.java:155) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:59) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:107) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:81) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:64) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.BpmnOverrideContextInterceptor.execute(BpmnOverrideContextInterceptor.java:26) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:53) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:83) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
... 90 more

Caused by: java.sql.SQLException: Connection is closed
at com.zaxxer.hikari.pool.ProxyConnection$ClosedConnection.lambda$getClosedConnection$0(ProxyConnection.java:489) ~[HikariCP-3.2.0.jar!/:?]
at com.sun.proxy.$Proxy186.prepareStatement(Unknown Source) ~[?:?]
at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:311) ~[HikariCP-3.2.0.jar!/:?]
at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java) ~[HikariCP-3.2.0.jar!/:?]
at sun.reflect.GeneratedMethodAccessor129.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_275]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_275]
at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:241) ~[spring-jdbc-5.1.18.RELEASE.jar!/:5.1.18.RELEASE]
at com.sun.proxy.$Proxy245.prepareStatement(Unknown Source) ~[?:?]
at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:86) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:59) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:87) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.flowable.common.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:271) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.db.DbSqlSession.queryWithRawParameter(DbSqlSession.java:250) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:165) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.persistence.entity.data.impl.MybatisExecutionDataManager.findProcessInstanceByQueryCriteria(MybatisExecutionDataManager.java:252) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.persistence.entity.ExecutionEntityManagerImpl.findProcessInstanceByQueryCriteria(ExecutionEntityManagerImpl.java:146) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.ProcessInstanceQueryImpl.executeList(ProcessInstanceQueryImpl.java:824) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.query.AbstractQuery.executeSingleResult(AbstractQuery.java:171) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.query.AbstractQuery.execute(AbstractQuery.java:155) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:59) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:107) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:81) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:64) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.BpmnOverrideContextInterceptor.execute(BpmnOverrideContextInterceptor.java:26) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:53) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:83) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
… 90 more

#####exception 2
Caused by: org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: java.sql.SQLException: Connection is closed

The error may exist in org/flowable/db/mapping/entity/ProcessDefinition.xml

The error may involve org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntityImpl.selectProcessDefinitionsByQueryCriteria

The error occurred while executing a query

SQL: select RES.* from ACT_RE_PROCDEF RES WHERE RES.KEY_ = ? and RES.VERSION_ = (select max(VERSION_) from ACT_RE_PROCDEF where KEY_ = RES.KEY_ and ( (TENANT_ID_ IS NOT NULL and TENANT_ID_ = RES.TENANT_ID_) or (TENANT_ID_ IS NULL and RES.TENANT_ID_ IS NULL) ) ) order by RES.ID_ asc

Cause: java.sql.SQLException: Connection is closed

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.flowable.common.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:271) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:234) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:158) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.persistence.entity.data.impl.MybatisProcessDefinitionDataManager.findProcessDefinitionsByQueryCriteria(MybatisProcessDefinitionDataManager.java:81) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntityManagerImpl.findProcessDefinitionsByQueryCriteria(ProcessDefinitionEntityManagerImpl.java:66) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.ProcessDefinitionQueryImpl.executeList(ProcessDefinitionQueryImpl.java:404) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.query.AbstractQuery.executeSingleResult(AbstractQuery.java:171) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.query.AbstractQuery.execute(AbstractQuery.java:155) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:59) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:107) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:81) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:64) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.BpmnOverrideContextInterceptor.execute(BpmnOverrideContextInterceptor.java:26) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:53) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:83) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
... 94 more

Caused by: java.sql.SQLException: Connection is closed
at com.zaxxer.hikari.pool.ProxyConnection$ClosedConnection.lambda$getClosedConnection$0(ProxyConnection.java:489) ~[HikariCP-3.2.0.jar!/:?]
at com.sun.proxy.$Proxy186.prepareStatement(Unknown Source) ~[?:?]
at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:311) ~[HikariCP-3.2.0.jar!/:?]
at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java) ~[HikariCP-3.2.0.jar!/:?]
at sun.reflect.GeneratedMethodAccessor129.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_275]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_275]
at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:241) ~[spring-jdbc-5.1.18.RELEASE.jar!/:5.1.18.RELEASE]
at com.sun.proxy.$Proxy245.prepareStatement(Unknown Source) ~[?:?]
at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:86) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:59) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:87) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.5.jar!/:3.5.5]
at org.flowable.common.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:271) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:234) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:158) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.persistence.entity.data.impl.MybatisProcessDefinitionDataManager.findProcessDefinitionsByQueryCriteria(MybatisProcessDefinitionDataManager.java:81) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntityManagerImpl.findProcessDefinitionsByQueryCriteria(ProcessDefinitionEntityManagerImpl.java:66) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.ProcessDefinitionQueryImpl.executeList(ProcessDefinitionQueryImpl.java:404) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.query.AbstractQuery.executeSingleResult(AbstractQuery.java:171) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.query.AbstractQuery.execute(AbstractQuery.java:155) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:59) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:107) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:81) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:64) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.engine.impl.interceptor.BpmnOverrideContextInterceptor.execute(BpmnOverrideContextInterceptor.java:26) ~[flowable-engine-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:53) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
at org.flowable.common.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:83) ~[flowable-engine-common-6.6.0.jar!/:6.6.0]
… 94 more

Hey @qqxadyy,

Is this the full stacktrace?

To me it seems like the transaction is taking way to long and the database is closing the transaction prematurely.

Cheers,
Filip

Thanks for reply. @filiphr
It is almost full stacktrace, I just deleted some business trace.

It happens on method whether I use @Transtional on not.
If the problem is as you sayd,can you help me to check the database config?

I use Mysql8, the configs of database and application’s datasource below:

It all depends how long it takes to execute the query you are doing? How much data are you pulling with that query. It is possible that your transaction is taking too long. You should be able to determine that based on your database configuration, each database has different way of configuring that.

In any case I would look into why your transaction is staying so open, from a business point of view.

Cheers,
Filip