Search in sources :

Example 1 with TransactionManager

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());
    }
}
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 2 with TransactionManager

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;
}
Also used : TransactionManager(org.drools.persistence.api.TransactionManager) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 3 with TransactionManager

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());
    }
}
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) NamingException(javax.naming.NamingException) RollbackException(javax.transaction.RollbackException) TransientObjectException(org.hibernate.TransientObjectException) Test(org.junit.Test)

Aggregations

TransactionManager (org.drools.persistence.api.TransactionManager)3 NamingException (javax.naming.NamingException)2 EntityManager (javax.persistence.EntityManager)2 DroolsPersistenceUtil.createEnvironment (org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment)2 Test (org.junit.Test)2 Environment (org.kie.api.runtime.Environment)2 InitialContext (javax.naming.InitialContext)1 RollbackException (javax.transaction.RollbackException)1 TransientObjectException (org.hibernate.TransientObjectException)1