数据库连接自动关闭

时间:2013-05-15 05:29:21

标签: postgresql activiti

我正在使用Postgres数据库。有时连接会在与我的应用程序交互时自动关闭。有时甚至我没有与我的应用程序进行交互,当时理想也连接已关闭。它显示了以下例外情况。我的服务器日志是

SEVERE [org.activiti.engine.impl.jobexecutor.AcquireJobsRunnable] (Thread-101) exception during job acquisition:

Error querying database.  Cause: org.postgresql.util.PSQLException: This connection has been closed.

The error may exist in org/activiti/db/mapping/entity/Job.xml

The error may involve org.activiti.engine.impl.persistence.entity.JobEntity.selectNextJobsToExecute
The error occurred while executing a query

SQL: select       RES.*            from ACT_RU_JOB RES          LEFT OUTER JOIN ACT_RU_EXECUTION PI ON PI.ID_ = RES.PROCESS_INSTANCE_ID_     where (RES.RETR
IES_ > 0)       and (RES.DUEDATE_ is null or RES.DUEDATE_ < ?)       and (RES.LOCK_OWNER_ is null or RES.LOCK_EXP_TIME_ < ?)    and (         (RES.EXECUTION_ID_
is null)      or       (PI.SUSPENSION_STATE_ = 1)       )       LIMIT ? OFFSET ?

Cause: org.postgresql.util.PSQLException: This connection has been closed.: org.apache.ibatis.exceptions.PersistenceException:

Error querying database.  Cause: org.postgresql.util.PSQLException: This connection has been closed.

The error may exist in org/activiti/db/mapping/entity/Job.xml
The error may involve org.activiti.engine.impl.persistence.entity.JobEntity.selectNextJobsToExecute

执行查询时发生错误

SQL: select       RES.*            from ACT_RU_JOB RES          LEFT OUTER JOIN ACT_RU_EXECUTION PI ON PI.ID_ = RES.PROCESS_INSTANCE_ID_     where (RES.RETR
IES_ > 0)       and (RES.DUEDATE_ is null or RES.DUEDATE_ < ?)       and (RES.LOCK_OWNER_ is null or RES.LOCK_EXP_TIME_ < ?)    and (         (RES.EXECUTION_ID_
is null)      or       (PI.SUSPENSION_STATE_ = 1)       )       LIMIT ? OFFSET ?

Cause: org.postgresql.util.PSQLException: This connection has been closed.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95) [mybatis-3.1.1.jar:3.1.1]
at org.activiti.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:301) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:292) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:287) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:274) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.persistence.entity.JobManager.findNextJobsToExecute(JobManager.java:105) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.cmd.AcquireJobsCmd.execute(AcquireJobsCmd.java:50) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.cmd.AcquireJobsCmd.execute(AcquireJobsCmd.java:33) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:60) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:32) [activiti-engine-5.11.jar:5.11]
at org.activiti.engine.impl.jobexecutor.AcquireJobsRunnable.run(AcquireJobsRunnable.java:61) [activiti-engine-5.11.jar:5.11]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_32]
Caused by: org.postgresql.util.PSQLException: This connection has been closed.
at org.postgresql.jdbc2.AbstractJdbc2Connection.checkClosed(AbstractJdbc2Connection.java:714)
at org.postgresql.jdbc3.AbstractJdbc3Connection.prepareStatement(AbstractJdbc3Connection.java:274)
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.doPrepareStatement(BaseWrapperManagedConnection.java:677)
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.prepareStatement(BaseWrapperManagedConnection.java:663)
at org.jboss.jca.adapters.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:404)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:72) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:82) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:54) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:70) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:56) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:141) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81) [mybatis-3.1.1.jar:3.1.1]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:101) [mybatis-3.1.1.jar:3.1.1]
... 13 more

2 个答案:

答案 0 :(得分:2)

我在使用PHP时遇到过类似的问题。问题的原因是我正在嵌套连接。在内部连接返回其数据后,返回的外部连接被关闭。 解决方案很简单:以串行方式处理两个连接,这意味着在这种情况下,我首先执行先前的内部连接,然后执行原始外部连接。

符号表示:

try{
  firstConnection = createConnection();
  results = firstConnection.query(...);
  if results == true then
    secondConnection = createConnection();
    resultsToo = secondConnection.query();
    if resultsToo == true then
      processResults(resultsToo);
      closeConnection();
    end if;
    processResults(results);
    closeConnection();
  end if;
exceptionHandling();

在第一次连接之前处理第二个连接解决了我的问题。

答案 1 :(得分:0)

可以通过在默认的Activiti数据源配置中删除此选项来解决问题

<property name="defaultAutoCommit" value="false" />