@Override
  protected StatefulKnowledgeSession createSession(KnowledgeBase kbase) {

    EnvironmentBuilder envBuilder = new KnowledgeSessionStorageEnvironmentBuilder(storage);
    Environment env = KieServices.Factory.get().newEnvironment();
    env.set(EnvironmentName.TRANSACTION_MANAGER, envBuilder.getTransactionManager());
    env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, envBuilder.getPersistenceContextManager());

    return JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
  }
  @Override
  protected StatefulKnowledgeSession disposeAndReloadSession(
      StatefulKnowledgeSession ksession, KnowledgeBase kbase) {
    long sessionId = ksession.getIdentifier();
    ksession.dispose();
    EnvironmentBuilder envBuilder = new KnowledgeSessionStorageEnvironmentBuilder(storage);
    Environment env = KieServices.Factory.get().newEnvironment();
    env.set(EnvironmentName.TRANSACTION_MANAGER, envBuilder.getTransactionManager());
    env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, envBuilder.getPersistenceContextManager());

    return JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
  }
 private void setupPersistence() {
   context = DroolsPersistenceUtil.setupWithPoolingDataSource(DROOLS_PERSISTENCE_UNIT_NAME);
   env = createEnvironment(context);
   if (locking) {
     env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
   }
 }
 @Before
 public void setUp() throws Exception {
   context = setupWithPoolingDataSource(JBPM_PERSISTENCE_UNIT_NAME);
   Environment env = EnvironmentFactory.newEnvironment();
   env.set(
       EnvironmentName.ENTITY_MANAGER_FACTORY,
       context.get(EnvironmentName.ENTITY_MANAGER_FACTORY));
   logService = new JPAAuditLogService(env);
 }
  public void initTransactionManager(Environment env) {
    Object tm = env.get(EnvironmentName.TRANSACTION_MANAGER);
    if (env.get(EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER) != null
        && env.get(EnvironmentName.TRANSACTION_MANAGER) != null) {
      this.txm = (TransactionManager) tm;
      this.tpm =
          (TaskPersistenceContextManager) env.get(EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER);
    } else {
      if (tm != null && isSpringTransactionManager(tm.getClass())) {
        try {
          logger.debug("Instantiating KieSpringTransactionManager");
          Class<?> cls = Class.forName("org.kie.spring.persistence.KieSpringTransactionManager");
          Constructor<?> con = cls.getConstructors()[0];
          this.txm = (TransactionManager) con.newInstance(tm);
          env.set(EnvironmentName.TRANSACTION_MANAGER, this.txm);
          cls = Class.forName("org.kie.spring.persistence.KieSpringTaskJpaManager");
          con = cls.getConstructors()[0];
          this.tpm = (TaskPersistenceContextManager) con.newInstance(new Object[] {env});
        } catch (Exception e) {

          logger.warn("Could not instantiate DroolsSpringTransactionManager");
          throw new RuntimeException(
              "Could not instantiate org.kie.container.spring.beans.persistence.DroolsSpringTransactionManager",
              e);
        }
      } else {
        logger.debug("Instantiating JtaTransactionManager");
        this.txm =
            new JtaTransactionManager(
                env.get(EnvironmentName.TRANSACTION),
                env.get(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY),
                tm);
        env.set(EnvironmentName.TRANSACTION_MANAGER, this.txm);
        try {
          this.tpm = new JPATaskPersistenceContextManager(env);
        } catch (Exception e) {
          throw new RuntimeException("Error creating JPATaskPersistenceContextManager", e);
        }
      }
      env.set(EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER, this.tpm);
      env.set(EnvironmentName.TRANSACTION_MANAGER, this.txm);
    }
  }
 protected StatefulKnowledgeSession createKnowledgeSession(KnowledgeBase kbase) {
   if (context == null) {
     context = DroolsPersistenceUtil.setupWithPoolingDataSource(DROOLS_PERSISTENCE_UNIT_NAME);
   }
   KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
   Environment env = createEnvironment(context);
   if (this.locking) {
     env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
   }
   return JPAKnowledgeService.newStatefulKnowledgeSession(kbase, ksconf, env);
 }
  @Before
  public void setUp() throws Exception {
    String methodName = testName.getMethodName();
    // Rules marshalling uses ObjectTypeNodes which screw up marshalling.
    if ("testLocalTransactionPerStatement".equals(methodName)
        || "testUserTransactions".equals(methodName)
        || "testPersistenceRuleSet".equals(methodName)
        || "testSetFocus".equals(methodName)
        // Constraints in ruleflows are rules as well (I'm guessing?), so OTN's again..
        || "testPersistenceState".equals(methodName)) {
      context = setupWithPoolingDataSource(JBPM_PERSISTENCE_UNIT_NAME, false);
    } else {
      context = setupWithPoolingDataSource(JBPM_PERSISTENCE_UNIT_NAME);
    }

    env = createEnvironment(context);
    if (useLocking) {
      env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    }
  }