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());
}
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());
}
}
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();
}
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);
}
}
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);
}
Aggregations