use of org.drools.persistence.api.TransactionManager 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.drools.persistence.api.TransactionManager in project drools by kiegroup.
the class JtaTransactionManager method findTransactionManager.
protected javax.transaction.TransactionManager findTransactionManager(UserTransaction ut) {
if (ut instanceof javax.transaction.TransactionManager) {
logger.debug("JTA UserTransaction object [{}] implements TransactionManager", ut);
return (javax.transaction.TransactionManager) ut;
}
InitialContext context = null;
try {
context = new InitialContext();
} catch (NamingException ex) {
logger.debug("Could not initialise JNDI InitialContext", ex);
return null;
}
// Check fallback JNDI locations.
for (String jndiName : FALLBACK_TRANSACTION_MANAGER_NAMES) {
if (jndiName == null) {
continue;
}
try {
javax.transaction.TransactionManager tm = (javax.transaction.TransactionManager) context.lookup(jndiName);
logger.debug("JTA TransactionManager found at fallback JNDI location [{}]", jndiName);
return tm;
} catch (NamingException ex) {
logger.debug("No JTA TransactionManager found at fallback JNDI location [{}]", jndiName);
}
}
// OK, so no JTA TransactionManager is available...
return null;
}
use of org.drools.persistence.api.TransactionManager in project drools by kiegroup.
the class JtaTransactionManagerTest method basicTransactionRollbackTest.
@Test
public void basicTransactionRollbackTest() {
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");
TransactionTestObject subObject = new TransactionTestObject();
subObject.setName("sub");
mainObject.setSubObject(subObject);
EntityManager em = emf.createEntityManager();
try {
boolean txOwner = txm.begin();
boolean notTxOwner = txm.begin();
em.persist(mainObject);
txm.rollback(notTxOwner);
em.persist(subObject);
txm.rollback(txOwner);
} catch (Exception e) {
fail("There should not be an exception thrown here: " + e.getMessage());
}
}
Aggregations