javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

Posted on

Hi Everyone,

I am getting the below error when I start a process.

below is the error and code snippet:

Hibernate: update SessionInfo set lastModificationDate=?, rulesByteArray=?, startDate=?, OPTLOCK=? where id=? and OPTLOCK=?
2020-11-06 10:12:09.181 ERROR 65864 --- [tp1122661733-24] o.s.t.s.TransactionSynchronizationUtils : TransactionSynchronization.beforeCompletion threw exception

javax.persistence.OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [org.drools.persistence.info.SessionInfo#1]
at org.hibernate.internal.ExceptionConverterImpl.wrapStaleStateException(ExceptionConverterImpl.java:226) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]

Code snippet:

Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault()
.entityManagerFactory(Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa"))
.userGroupCallback(new JBossUserGroupCallbackImpl("classpath:/usergroup.properties"))
.addAsset(ResourceFactory.newClassPathResource("process.bpmn"), ResourceType.BPMN2)
.addEnvironmentEntry(EnvironmentName.TRANSACTION_MANAGER, jtaPlatform)
.get();
RuntimeManager manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
RuntimeEngine runtimeEngine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession)manager.getRuntimeEngine(EmptyContext.get()).getKieSession();
//KieSession kieSession = runtimeEngine.getKieSession();
System.out.println("All processes == "+((InternalRuntimeManager)manager).getEnvironment().getKieBase().getProcesses().contains(processId));
ksession.startProcess(processId);
manager.disposeRuntimeEngine(runtimeEngine);

Persistence.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

org.hibernate.ejb.HibernatePersistence

    <!--<jta-data-source>jdbc/jbpm-ds</jta-data-source>-->
    <mapping-file>META-INF/JBPMorm.xml</mapping-file>

    <class>org.drools.persistence.info.SessionInfo</class>
    <class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
    <class>org.drools.persistence.info.WorkItemInfo</class>
    <class>org.jbpm.persistence.correlation.CorrelationKeyInfo</class>
    <class>org.jbpm.persistence.correlation.CorrelationPropertyInfo</class>
    <class>org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo</class>
    <class>org.jbpm.process.audit.ProcessInstanceLog</class>
    <class>org.jbpm.process.audit.NodeInstanceLog</class>
    <class>org.jbpm.process.audit.VariableInstanceLog</class>
    <class>org.jbpm.services.task.impl.model.UserImpl</class>
    <class>org.jbpm.services.task.impl.model.GroupImpl</class>
    <class>org.jbpm.services.task.impl.model.TaskImpl</class>
    <class>org.jbpm.services.task.impl.model.CommentImpl</class>
    <class>org.jbpm.services.task.impl.model.AttachmentImpl</class>
    <class>org.jbpm.services.task.impl.model.I18NTextImpl</class>
    <class>org.jbpm.services.task.impl.model.ContentImpl</class>
    <class>org.jbpm.services.task.impl.model.BooleanExpressionImpl</class>
    <class>org.jbpm.services.task.impl.model.DeadlineImpl</class>
    <class>org.jbpm.services.task.impl.model.DelegationImpl</class>
    <class>org.jbpm.services.task.impl.model.EscalationImpl</class>
    <class>org.jbpm.services.task.impl.model.NotificationImpl</class>
    <class>org.jbpm.services.task.impl.model.EmailNotificationImpl</class>
    <class>org.jbpm.services.task.impl.model.EmailNotificationHeaderImpl</class>
    <class>org.jbpm.services.task.impl.model.PeopleAssignmentsImpl</class>
    <class>org.jbpm.services.task.impl.model.ReassignmentImpl</class>
    <class>org.jbpm.services.task.impl.model.TaskDataImpl</class>
    <class>org.jbpm.services.task.audit.impl.model.AuditTaskImpl</class>
    <class>org.jbpm.services.task.audit.impl.model.TaskEventImpl</class>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:h2:mem:jbpm-db;MVCC=true"/>
        <property name="hibernate.connection.driver_class" value="org.h2.Driver"></property>
        <property name="hibernate.connection.username" value="sa"/>
        <property name="hibernate.connection.password" value="sa"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        <property name="hibernate.connection.autocommit" value="false" />
        <property name="hibernate.max_fetch_depth" value="3"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
        <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
       </properties>
</persistence-unit>