@Test
  public void testPersistenceTimer2() throws Exception {
    setUp();

    Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());

    SessionConfiguration config = new SessionConfiguration(properties);
    config.setTimerJobFactoryManager(new JpaTimeJobFactoryManager());

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KnowledgePackage> kpkgs = getProcessTimer2();
    kbase.addKnowledgePackages(kpkgs);

    SingleSessionCommandService service = new SingleSessionCommandService(kbase, config, env);
    int sessionId = service.getSessionId();
    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    ProcessInstance processInstance = service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());

    Thread.sleep(2000);

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
  }
  @Override
  public Map<String, WorkItemHandler> getWorkItemHandlers(
      String identifier, Map<String, Object> params) {
    HashMap<String, WorkItemHandler> wihMap = new HashMap<String, WorkItemHandler>();
    for (AbstractBidsWorkItemHandlerProducer producer : allProducers)
      wihMap.putAll(producer.getWorkItemHandlers(identifier, params));

    // hack to register interceptor
    KieSession kieSession = getKieSession(params);
    SingleSessionCommandService sscs =
        (SingleSessionCommandService)
            ((CommandBasedStatefulKnowledgeSession) kieSession).getCommandService();

    sscs.addInterceptor(new TransactionLockInterceptor(kieSession.getEnvironment()));

    return wihMap;
  }
  @Test
  @Ignore("Probably expects H2 do delete everything on shutdown?")
  public void testPersistenceTimer2() throws Exception {
    final Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(
        EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());

    final Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    final SessionConfiguration config = new SessionConfiguration(properties);

    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    final Collection<KnowledgePackage> kpkgs = getProcessTimer2();
    kbase.addKnowledgePackages(kpkgs);

    SingleSessionCommandService service = new SingleSessionCommandService(kbase, config, env);
    final int sessionId = service.getSessionId();
    final StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    ProcessInstance processInstance = service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());

    Thread.sleep(2000);

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    final GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
  }
  @Test
  public void testPersistenceWorkItems() throws Exception {
    setUp();

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KnowledgePackage> kpkgs = getProcessWorkItems();
    kbase.addKnowledgePackages(kpkgs);

    Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    SessionConfiguration config = new SessionConfiguration(properties);

    SingleSessionCommandService service = new SingleSessionCommandService(kbase, config, env);
    int sessionId = service.getSessionId();

    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    ProcessInstance processInstance = service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());

    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);

    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);

    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);

    workItem = handler.getWorkItem();
    assertNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
  }
  @Test
  public void testPersistenceSubProcess() {
    setUp();

    Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    SessionConfiguration config = new SessionConfiguration(properties);

    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    Package pkg = getProcessSubProcess();
    ruleBase.addPackage(pkg);

    SingleSessionCommandService service = new SingleSessionCommandService(ruleBase, config, env);
    int sessionId = service.getSessionId();
    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    RuleFlowProcessInstance processInstance =
        (RuleFlowProcessInstance) service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());
    long processInstanceId = processInstance.getId();

    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);

    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    SubProcessNodeInstance subProcessNodeInstance =
        (SubProcessNodeInstance) nodeInstances.iterator().next();
    long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    RuleFlowProcessInstance subProcessInstance =
        (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(subProcessInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(subProcessInstance);

    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
  }
  @Test
  public void testPersistenceWorkItemsUserTransaction() throws Exception {
    final Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(
        EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());

    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    final Collection<KnowledgePackage> kpkgs = getProcessWorkItems();
    kbase.addKnowledgePackages(kpkgs);

    final Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    final SessionConfiguration config = new SessionConfiguration(properties);

    SingleSessionCommandService service = new SingleSessionCommandService(kbase, config, env);
    final int sessionId = service.getSessionId();

    final UserTransaction ut =
        (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    final StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    ProcessInstance processInstance = service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());
    ut.commit();

    final TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    ut.commit();
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    ut.commit();

    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    ut.commit();
    assertNotNull(processInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    ut.commit();

    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    ut.commit();
    assertNotNull(processInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    ut.commit();

    workItem = handler.getWorkItem();
    assertNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, kbase, config, env);
    ut.begin();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    ut.commit();
    assertNull(processInstance);
    service.dispose();
  }