Search in sources :

Example 6 with Environment

use of org.kie.api.runtime.Environment in project drools by kiegroup.

the class TransactionManagerFactoryTest method createsJtaTransactionManagerWithEnvironment.

@Test
public void createsJtaTransactionManagerWithEnvironment() throws Exception {
    Environment env = EnvironmentFactory.newEnvironment();
    assertEquals(JtaTransactionManagerFactory.class.getName(), transactionManagerFactory.get().getClass().getName());
    assertNotNull(transactionManagerFactory.newTransactionManager(env));
    assertEquals(JtaTransactionManager.class.getName(), transactionManagerFactory.newTransactionManager(env).getClass().getName());
}
Also used : JtaTransactionManagerFactory(org.drools.persistence.jta.JtaTransactionManagerFactory) JtaTransactionManager(org.drools.persistence.jta.JtaTransactionManager) Environment(org.kie.api.runtime.Environment) Test(org.junit.Test)

Example 7 with Environment

use of org.kie.api.runtime.Environment in project drools by kiegroup.

the class JtaTransactionManagerTest method basicTransactionManagerTest.

@Test
public void basicTransactionManagerTest() {
    String testName = getTestName();
    // Setup the JtaTransactionmanager
    Environment env = createEnvironment(context);
    Object tm = env.get(EnvironmentName.TRANSACTION_MANAGER);
    TransactionManager txm = new JtaTransactionManager(env.get(EnvironmentName.TRANSACTION), env.get(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY), tm);
    // Create linked transactionTestObjects
    TransactionTestObject mainObject = new TransactionTestObject();
    mainObject.setName("main" + testName);
    TransactionTestObject subObject = new TransactionTestObject();
    subObject.setName("sub" + testName);
    mainObject.setSubObject(subObject);
    // Commit the mainObject after "commiting" the subObject
    EntityManager em = emf.createEntityManager();
    try {
        // Begin the real trasnaction
        boolean txOwner = txm.begin();
        // Do the "sub" transaction
        // - the txm doesn't really commit,
        // because we keep track of who's the tx owner.
        boolean notTxOwner = txm.begin();
        em.persist(mainObject);
        txm.commit(notTxOwner);
        // Finish the transaction off
        em.persist(subObject);
        txm.commit(txOwner);
    } catch (Throwable t) {
        fail("No exception should have been thrown: " + t.getMessage());
    }
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionManager(org.drools.persistence.api.TransactionManager) DroolsPersistenceUtil.createEnvironment(org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) Test(org.junit.Test)

Example 8 with Environment

use of org.kie.api.runtime.Environment in project drools by kiegroup.

the class JtaTransactionManagerTest method testSingleSessionCommandServiceAndJtaTransactionManagerTogether.

@Test
public void testSingleSessionCommandServiceAndJtaTransactionManagerTogether() throws Exception {
    // Initialize drools environment stuff
    Environment env = createEnvironment(context);
    KieBase kbase = initializeKnowledgeBase(simpleRule);
    KieSession commandKSession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
    // StatefulKnowledgeSession commandKSession = JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );
    // initialize CSEM
    commandKSession.getIdentifier();
    PersistableRunner commandService = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) commandKSession).getRunner();
    JpaPersistenceContextManager jpm = (JpaPersistenceContextManager) getValueOfField("jpm", commandService);
    TransactionTestObject mainObject = new TransactionTestObject();
    mainObject.setName("mainCommand");
    TransactionTestObject subObject = new TransactionTestObject();
    subObject.setName("subCommand");
    mainObject.setSubObject(subObject);
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    ut.begin();
    HashMap<String, Object> emEnv = new HashMap<String, Object>();
    emEnv.put(COMMAND_ENTITY_MANAGER_FACTORY, emf);
    emEnv.put(COMMAND_ENTITY_MANAGER, jpm.getCommandScopedEntityManager());
    TransactionTestCommand txTestCmd = new TransactionTestCommand(mainObject, subObject, emEnv);
    commandKSession.execute(txTestCmd);
    ut.commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) DroolsPersistenceUtil.createEnvironment(org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner) JpaPersistenceContextManager(org.drools.persistence.jpa.JpaPersistenceContextManager) Test(org.junit.Test)

Example 9 with Environment

use of org.kie.api.runtime.Environment in project drools by kiegroup.

the class JpaBasedPersistenceTest method setUp.

@Before
public void setUp() throws Exception {
    context = DroolsPersistenceUtil.setupWithPoolingDataSource(DROOLS_PERSISTENCE_UNIT_NAME);
    emf = (EntityManagerFactory) context.get(ENTITY_MANAGER_FACTORY);
    if (useTransactions()) {
        useTransactions = true;
        Environment env = createEnvironment(context);
        Object tm = env.get(EnvironmentName.TRANSACTION_MANAGER);
        this.txm = new JtaTransactionManager(env.get(EnvironmentName.TRANSACTION), env.get(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY), tm);
    }
}
Also used : JtaTransactionManager(org.drools.persistence.jta.JtaTransactionManager) DroolsPersistenceUtil.createEnvironment(org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) Before(org.junit.Before)

Example 10 with Environment

use of org.kie.api.runtime.Environment in project drools by kiegroup.

the class MapBasedPersistenceTest method disposeAndReloadSession.

@Override
protected KieSession disposeAndReloadSession(KieSession ksession, KieBase 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);
}
Also used : KnowledgeSessionStorageEnvironmentBuilder(org.drools.persistence.map.KnowledgeSessionStorageEnvironmentBuilder) EnvironmentBuilder(org.drools.persistence.map.EnvironmentBuilder) KnowledgeSessionStorageEnvironmentBuilder(org.drools.persistence.map.KnowledgeSessionStorageEnvironmentBuilder) Environment(org.kie.api.runtime.Environment)

Aggregations

Environment (org.kie.api.runtime.Environment)31 Test (org.junit.Test)15 KieBase (org.kie.api.KieBase)9 KieSession (org.kie.api.runtime.KieSession)9 ArrayList (java.util.ArrayList)6 DroolsPersistenceUtil.createEnvironment (org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment)6 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 IdentityPlaceholderResolverStrategy (org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy)4 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)4 Cheese (org.drools.compiler.Cheese)3 PersistenceContext (org.drools.persistence.api.PersistenceContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Collection (java.util.Collection)2 List (java.util.List)2 EntityManager (javax.persistence.EntityManager)2 UserTransaction (javax.transaction.UserTransaction)2 Person (org.drools.compiler.Person)2 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)2