Sounds like you are accessing to the same session from different threads and just one of them can complete the transaction. That's the expected behavior.
Greetings.

On Wed, Nov 24, 2010 at 10:40 AM, <darren.kay@bt.com> wrote:
Drools Version: 5.1.0
JBoss Version: 5.1.0 GA (Clustered)

We're using a clustered JBoss setup (with 2 nodes) with Drools and getting an intermittent stale ksession exception - stack trace attached below.

We use 'org.drools.persistence.jpa.JPAKnowledgeService' to persist KnowledgeSession to a MySQL database and use Hibernate to manage the transactions.

In case of a new request, we create a new Drools Knowledge Session and persist it and store the knowledge session id to our database.

StatefulKnowledgeSession session = JPAKnowledgeService.newStatefulKnowledgeSession(knowledgeBase, null, environment);
return session;

For all subsequent requests we load the Knowledge session from the database using the knowledge session id.

StatefulKnowledgeSession session = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, knowledgeBase, null,     environment);
return session;

We receive request from the front-end validate the information and update the knowledge session facts using;

public void insertOrUpdateSessionFacts(StatefulKnowledgeSession session, Object...objects) {

      boolean factMatched = false;

      for (Object obj: objects) {

              //get all session facts
              Collection<Object> facts = session.getObjects();

              for (Object fact: facts) {
                      if (fact.getClass().toString().equals(obj.getClass().toString())) {
                              //update fact in session
                              session.update(session.getFactHandle(fact), obj);
                              factMatched = true;
                              break;
                      }
              }

              if (factMatched) {
                      factMatched = false;
              } else {
                      //fact not found, so insert new fact
                      session.insert(obj);
              }
      }
}

After the facts are updated we use find the response variable populated from the Drools flow and pass it back to the front-end;

//get variable scope context
VariableScopeInstance vi = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
return vi.getVariable(variableName);

Any pointers would be much appreciated Stack Trace is below

Darren

-------------------------------------

2010-11-23 04:40:26,838 INFO  [STDOUT] (pool-47-thread-1) Hibernate: select workitemin0_.workItemId as workItemId92_0_, workitemin0_.creationDate as creation2_92_0_, workitemin0_.externalVariables as external3_92_0_, workitemin0_.name as name92_0_, workitemin0_.processInstanceId as processI5_92_0_, workitemin0_.state as state92_0_, workitemin0_.OPTLOCK as OPTLOCK92_0_, workitemin0_.workItemByteArray as workItem8_92_0_ from WorkItemInfo workitemin0_ where workitemin0_.workItemId=?
2010-11-23 04:40:26,841 INFO  [STDOUT] (pool-47-thread-1) Hibernate: select variables0_.workItemId as workItemId1_, variables0_.id as id1_, variables0_.name as formula1_1_, variables0_.id as id94_0_, variables0_.name as name94_0_, variables0_.persister as persister94_0_, variables0_.processInstanceId as processI5_94_0_, variables0_.workItemId as workItemId94_0_ from VariableInstanceInfo variables0_ where variables0_.workItemId=?
2010-11-23 04:40:26,857 INFO  [STDOUT] (pool-47-thread-1) Hibernate: update ProcessInstanceInfo set externalVariables=?, lastModificationDate=?, lastReadDate=?, processId=?, processInstanceByteArray=?, startDate=?, state=?, OPTLOCK=? where InstanceId=? and OPTLOCK=?
2010-11-23 04:40:26,864 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] (pool-47-thread-1) Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [org.drools.persistence.processinstance.ProcessInstanceInfo#14]
      at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1792)
      at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2435)
      at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2335)
      at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2635)
      at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:115)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
      at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
      at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:101)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:269)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:89)
      at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1423)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:137)
      at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
      at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:170)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
      at org.jboss.aspects.tx.TxInterceptor$RequiresNew.invoke(TxInterceptor.java:261)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:421)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invokeLocal(IsLocalInterceptor.java:85)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:72)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.remoting.PojiProxy.invoke(PojiProxy.java:62)
      at $Proxy608.invoke(Unknown Source)
      at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
      at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
      at $Proxy607.processKbdRequest(Unknown Source)
      at net.plus.kbd.faults.gateway.actions.DroolsFlowSender.process(DroolsFlowSender.java:84)
      at sun.reflect.GeneratedMethodAccessor807.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.jboss.soa.esb.listeners.message.ActionProcessorMethodInfo.processMethods(ActionProcessorMethodInfo.java:102)
      at org.jboss.soa.esb.listeners.message.OverriddenActionLifecycleProcessor.process(OverriddenActionLifecycleProcessor.java:74)
      at org.jboss.soa.esb.listeners.message.ActionProcessingPipeline.processPipeline(ActionProcessingPipeline.java:649)
      at org.jboss.soa.esb.listeners.message.ActionProcessingPipeline.processPipeline(ActionProcessingPipeline.java:603)
      at org.jboss.soa.esb.listeners.message.ActionProcessingPipeline.process(ActionProcessingPipeline.java:433)
      at org.jboss.soa.esb.listeners.message.MessageAwareListener$TransactionalRunner.run(MessageAwareListener.java:540)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
2010-11-23 04:40:26,871 WARN  [com.arjuna.ats.arjuna.logging.arjLoggerI18N] (pool-47-thread-1) [com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator_2] TwoPhaseCoordinator.beforeCompletion - failed for com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple@c9c803
javax.persistence.OptimisticLockException: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [org.drools.persistence.processinstance.ProcessInstanceInfo#14]
      at org.hibernate.ejb.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:630)
      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:588)
      at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:513)
      at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:101)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:269)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:89)
      at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1423)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:137)
      at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
      at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:170)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
at org.jboss.aspects.tx.TxInterceptor$RequiresNew.invoke(TxInterceptor.java:261)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:421)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invokeLocal(IsLocalInterceptor.java:85)
      at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:72)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
      at org.jboss.aspects.remoting.PojiProxy.invoke(PojiProxy.java:62)
      at $Proxy608.invoke(Unknown Source)
      at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
      at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
      at $Proxy607.processKbdRequest(Unknown Source)
      at net.plus.kbd.faults.gateway.actions.DroolsFlowSender.process(DroolsFlowSender.java:84)
      at sun.reflect.GeneratedMethodAccessor807.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.jboss.soa.esb.listeners.message.ActionProcessorMethodInfo.processMethods(ActionProcessorMethodInfo.java:102)
      at org.jboss.soa.esb.listeners.message.OverriddenActionLifecycleProcessor.process(OverriddenActionLifecycleProcessor.java:74)
      at org.jboss.soa.esb.listeners.message.ActionProcessingPipeline.processPipeline(ActionProcessingPipeline.java:649)
      at org.jboss.soa.esb.listeners.message.ActionProcessingPipeline.processPipeline(ActionProcessingPipeline.java:603)
      at org.jboss.soa.esb.listeners.message.ActionProcessingPipeline.process(ActionProcessingPipeline.java:433)
      at org.jboss.soa.esb.listeners.message.MessageAwareListener$TransactionalRunner.run(MessageAwareListener.java:540)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [org.drools.persistence.processinstance.ProcessInstanceInfo#14]
      at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1792)
      at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2435)
      at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2335)
      at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2635)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:115)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
      at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
      ... 45 more



_______________________________________________
rules-dev mailing list
rules-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-dev


_______________________________________________
rules-users mailing list
https://lists.jboss.org/mailman/listinfo/rules-users



--
 - CTO @ http://www.plugtree.com 
 - MyJourney @ http://salaboy.wordpress.com
 - Co-Founder @ http://www.jbug.com.ar
 
 - Salatino "Salaboy" Mauricio -