flowable 6.4.1 FlowableOptimisticLockingException
flowable FlowableOptimisticLockingException
最近使用flowable开发时,由于业务代码使用了 @Transactional 导致保存业务变量时出错,提示如问题描述
问题描述
xxx was updated by another transaction concurrently
2019-11-19 09:40:11.671 ERROR[ad-196]c.z.o.f.s.impl.ApplyServiceImpl.approvePass<2003> approvePass Execption :{}
org.flowable.common.engine.api.FlowableOptimisticLockingException: Task[id=316750988787904512, name=流程管理员] was updated by another transaction concurrently
at org.flowable.common.engine.impl.db.DbSqlSession.flushUpdates(DbSqlSession.java:505)
at org.flowable.common.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:292)
at org.flowable.common.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:191)
at org.flowable.common.engine.impl.interceptor.CommandContext.close(CommandContext.java:61)
at org.flowable.common.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:80)
问题分析
由于flowable处理并发时,是属于伪并发,采用乐观锁机制处理。在由同一数据条目的并发访问引起的数据存储中发生乐观锁定时抛出。
源代码分析
解决方案
根据实际需要设置事务隔离和传播
@Transactional(rollbackFor = {RuntimeException.class, Exception.class}, propagation = Propagation.NESTED,isolation = Isolation.READ_UNCOMMITTED)
propagation 属性
事务的传播行为,默认值为 Propagation.REQUIRED。
可选的值有:
Propagation.REQUIRED
如果当前存在事务,则加入该事务,如果当前不存在事务,则创建一个新的事务。
Propagation.SUPPORTS
如果当前存在事务,则加入该事务;如果当前不存在事务,则以非事务的方式继续运行。
Propagation.MANDATORY
如果当前存在事务,则加入该事务;如果当前不存在事务,则抛出异常。
Propagation.REQUIRES_NEW
重新创建一个新的事务,如果当前存在事务,暂停当前的事务。
Propagation.NOT_SUPPORTED
以非事务的方式运行,如果当前存在事务,暂停当前的事务。
Propagation.NEVER
以非事务的方式运行,如果当前存在事务,则抛出异常。
Propagation.NESTED
和 Propagation.REQUIRED 效果一样。
isolation 属性
事务的隔离级别,默认值为 Isolation.DEFAULT。
可选的值有:
Isolation.DEFAULT
使用底层数据库默认的隔离级别。
Isolation.READ_UNCOMMITTED
Isolation.READ_COMMITTED
Isolation.REPEATABLE_READ
Isolation.SERIALIZABLE