cancel
Showing results for 
Search instead for 
Did you mean: 

Deadlock in Junit test

pika822
Champ in-the-making
Champ in-the-making
Hello,

Excus eme if this topic is a duplication, it's the third time I try to create it.
I have a random deadlock during a Junit test suite. The tests are executed sequentially.
The deadlock happens only for a specific use case test. According to my other traces, the deadlock appears just before an asynchronous Service task. However, other unit tests of the process which pass by this service task, don't have any problems.
My configuration :
H2 database
activiti 5.16.4


org.apache.ibatis.exceptions.PersistenceException:
### Error updating database.  Cause: org.h2.jdbc.JdbcSQLException: Deadlock detected. The current transaction was rolled back. Details: "
Session #4 (user: SA) is waiting to lock PUBLIC.ACT_HI_PROCINST while locking PUBLIC.ACT_HI_ACTINST (exclusive), PUBLIC.ACT_GE_BYTEARRAY (exclusive), PUBLIC.ACT_HI_VARINST (exclusive).
Session #3 (user: SA) is waiting to lock PUBLIC.ACT_HI_ACTINST while locking PUBLIC.ACT_RU_EXECUTION (exclusive), PUBLIC.ACT_HI_PROCINST (exclusive)."; SQL statement:
insert into ACT_HI_ACTINST (
        ID_,
        PROC_DEF_ID_,
        PROC_INST_ID_,
        EXECUTION_ID_,
        ACT_ID_,
        TASK_ID_,
        CALL_PROC_INST_ID_,
        ACT_NAME_,
        ACT_TYPE_,
        ASSIGNEE_,
        START_TIME_,
        END_TIME_,
        DURATION_,
        TENANT_ID_
      ) values (
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?
      ) [40001-172]
### The error may involve org.activiti.engine.impl.persistence.entity.HistoricActivityInstanceEntity.insertHistoricActivityInstance-Inline
### The error occurred while setting parameters
### SQL: insert into ACT_HI_ACTINST (         ID_,         PROC_DEF_ID_,         PROC_INST_ID_,         EXECUTION_ID_,         ACT_ID_,         TASK_ID_,         CALL_PROC_INST_ID_,         ACT_NAME_,         ACT_TYPE_,         ASSIGNEE_,         START_TIME_,         END_TIME_,         DURATION_,         TENANT_ID_       ) values (         ?,         ?,         ?,         ?,         ?,         ?,         ?,         ?,         ?,         ?,         ?,         ?,         ?,         ?       )
### Cause: org.h2.jdbc.JdbcSQLException: Deadlock detected. The current transaction was rolled back. Details: "
Session #4 (user: SA) is waiting to lock PUBLIC.ACT_HI_PROCINST while locking PUBLIC.ACT_HI_ACTINST (exclusive), PUBLIC.ACT_GE_BYTEARRAY (exclusive), PUBLIC.ACT_HI_VARINST (exclusive).
Session #3 (user: SA) is waiting to lock PUBLIC.ACT_HI_ACTINST while locking PUBLIC.ACT_RU_EXECUTION (exclusive), PUBLIC.ACT_HI_PROCINST (exclusive)."; SQL statement:
insert into ACT_HI_ACTINST (
        ID_,
        PROC_DEF_ID_,
        PROC_INST_ID_,
        EXECUTION_ID_,
        ACT_ID_,
        TASK_ID_,
        CALL_PROC_INST_ID_,
        ACT_NAME_,
        ACT_TYPE_,
        ASSIGNEE_,
        START_TIME_,
        END_TIME_,
        DURATION_,
        TENANT_ID_
      ) values (
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?
      ) [40001-172]
   at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
   at org.h2.message.DbException.get(DbException.java:169)
   at org.h2.message.DbException.get(DbException.java:146)
   at org.h2.table.RegularTable.doLock(RegularTable.java:504)
   at org.h2.table.RegularTable.lock(RegularTable.java:450)
   at org.h2.command.dml.Insert.insertRows(Insert.java:125)
   at org.h2.command.dml.Insert.update(Insert.java:86)
   at org.h2.command.CommandContainer.update(CommandContainer.java:79)
   at org.h2.command.Command.executeUpdate(Command.java:235)
   at org.h2.jdbc.JdbcPreparedStatement.execute(JdbcPreparedStatement.java:193)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
   at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:41)
   at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:66)
   at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:45)
   at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:100)
   at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
   at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:148)
   at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:137)
   at org.activiti.engine.impl.db.DbSqlSession.flushInserts(DbSqlSession.java:758)
   at org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:590)
   at org.activiti.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:211)
   at org.activiti.engine.impl.interceptor.CommandContext.close(CommandContext.java:137)
   at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:66)
   at org.activiti.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:47)
   at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
   at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:45)
   at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:31)
   at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:40)
   at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:35)
   at org.activiti.engine.impl.RuntimeServiceImpl.startProcessInstanceByKey(RuntimeServiceImpl.java:77)
   at com.myapplication.test.workflow.TestProcessAbstract.testAsynchronousProcessExecution(TestProcessAbstract.java:187)
   at com.myapplication.test.workflow.TestProcessAbstract.testAsynchronousProcessExecution(TestProcessAbstract.java:168)
   at com.myapplication.workflow.firstprocess.TestFirstProcess.testFirstProcessUseCaseX(TestFirstProcess.java:255)


I have no idea why this happens. I can only hope it won't appear for "true" database.
Thank you.
4 REPLIES 4

trademak
Star Contributor
Star Contributor
Are you using threading in this unit test? H2 is not a good database to test multithreaded behavior in the Engine. You would need to test that in a "true" database.

Best regards,

pika822
Champ in-the-making
Champ in-the-making
Thanks for your answer.
I don't think so. The unit test (which generates the error) has only one process instance in execution for this test. There are no parallel execution in the process definition, just an asynchronous service task. And the unit tests executions are sequential.
However, this error only appears when I execute all the tests of my Junit Class. It never appears when I launch just the unit test.

pika822
Champ in-the-making
Champ in-the-making
I didn't check on a real database.
However, my process is designed to use JPA variables, and in my unit test, I didn't persist my variable. So I fixed that problem and it resolved the deadlock problem too.

jbarrez
Star Contributor
Star Contributor
Ok, very odd. But the main thing is your problem is gone 🙂