Search in sources :

Example 6 with Account

use of org.jpox.samples.models.company.Account in project tests by datanucleus.

the class GeneralTest method testBasicJTAViaJDOTransaction.

/**
 * Test that uses JTA but starts the transaction using the JDO transaction methods.
 * See JDO2 16.1.3 for a brief description of the type of situation
 */
public void testBasicJTAViaJDOTransaction() throws Exception {
    try {
        UserTransaction ut = getUserTransaction();
        ut.setTransactionTimeout(300);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        assertEquals("Transaction type is not JTA!", "JTA", pmf.getTransactionType());
        int totals = 0;
        try {
            // Demarcate the txn using JDO txn
            tx.begin();
            try {
                // Try to start the UserTransaction now (should have been started by JDO txn)
                ut.begin();
                fail("Attempted call to UserTransaction.begin after starting JDO txn directly worked!!");
            } catch (Exception e) {
            // Expected since the JDO txn started the user transaction
            }
            assertTrue("Transaction is not active after starting UserTransaction", tx.isActive());
            Query q = pm.newQuery(Account.class);
            Collection c = (Collection) q.execute();
            totals = c.size();
            q.closeAll();
            tx.commit();
        } catch (Exception e) {
            LOG.info(">> Exception thrown ", e);
            fail("Exception thrown during use of JTA via JDO Transaction");
        } finally {
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            tx.begin();
            Account accnt = new Account();
            accnt.setUsername("jpox");
            pm.makePersistent(accnt);
            tx.commit();
        } finally {
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            tx.begin();
            Query q = pm.newQuery(Account.class);
            Collection c = (Collection) q.execute();
            try {
                Assert.assertTrue(c.size() == (totals + 1));
            } finally {
                q.closeAll();
                tx.commit();
            }
        } finally {
            pm.close();
        }
    } finally {
        clean(Account.class);
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Account(org.jpox.samples.models.company.Account) UserTransaction(javax.transaction.UserTransaction) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) NamingException(javax.naming.NamingException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 7 with Account

use of org.jpox.samples.models.company.Account in project tests by datanucleus.

the class AttachDetachTest method testDetachAttachWithNoChangeLifecycle.

/**
 * Test detach and then attach with no changes, and the effect on lifecycle states.
 */
public void testDetachAttachWithNoChangeLifecycle() {
    try {
        // Create object and detach it
        Employee woodyDetached = null;
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
            Account acct = new Account();
            acct.setId(101);
            acct.setEnabled(true);
            acct.setUsername("woodyw");
            woody.setAccount(acct);
            pm.makePersistent(woody);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Attach the object
        pm = newPM();
        tx = pm.currentTransaction();
        // Delay all persistence ops til flush/commit
        tx.setOptimistic(true);
        try {
            tx.begin();
            Employee woody = pm.makePersistent(woodyDetached);
            Account acct = woody.getAccount();
            assertFalse("Attached object is dirty but shouldn't be", JDOHelper.isDirty(woody));
            assertFalse("Attached Account is dirty but shouldn't be", JDOHelper.isDirty(acct));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Account(org.jpox.samples.models.company.Account) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 8 with Account

use of org.jpox.samples.models.company.Account in project tests by datanucleus.

the class AttachDetachTest method testPersistWithDetachedRelative.

/**
 * Test that persists an object, and then detaches it. Then creates an object and adds a relation
 * to the detached object and persists the new object. The related object should be connected to
 * the new object (shouldn't create a new version of the object).
 * @throws Exception Thrown if an error occurs.
 */
public void testPersistWithDetachedRelative() throws Exception {
    try {
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        Account detachedAcct = null;
        try {
            tx.begin();
            Account acct = new Account();
            acct.setEnabled(true);
            acct.setUsername("john");
            pm.makePersistent(acct);
            detachedAcct = (Account) pm.detachCopy(acct);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error("Exception in test", ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Persist the new object with the related detached
        // and detachCopy the new object
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
            woody.setAccount(detachedAcct);
            pm.makePersistent(woody);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error("Exception during test", ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Account(org.jpox.samples.models.company.Account) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 9 with Account

use of org.jpox.samples.models.company.Account in project tests by datanucleus.

the class GeneralTest method testMultiTxnJTA.

public void testMultiTxnJTA() throws Exception {
    try {
        UserTransaction ut = getUserTransaction();
        ut.setTransactionTimeout(300);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            ut.begin();
            Query q = pm.newQuery(Account.class);
            Collection c = (Collection) q.execute();
            try {
                Assert.assertEquals("should have no elements in db", 0, c.size());
            } finally {
                q.closeAll();
                ut.commit();
            }
        } finally {
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            ut.begin();
            Account accnt = new Account();
            accnt.setUsername("jpox");
            pm.makePersistent(accnt);
            ut.commit();
        } finally {
            pm.close();
        }
        // db has now 1 element
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            ut.begin();
            Account accnt = new Account();
            accnt.setUsername("jpox2");
            pm.makePersistent(accnt);
            pm.flush();
            ut.rollback();
        } finally {
            pm.close();
        }
        // db should still have 1 element
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            ut.begin();
            Query q = pm.newQuery(Account.class);
            Collection c = (Collection) q.execute();
            try {
                Assert.assertEquals(1, c.size());
            } finally {
                q.closeAll();
                ut.commit();
            }
        } finally {
            pm.close();
        }
    } finally {
        clean(Account.class);
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Account(org.jpox.samples.models.company.Account) UserTransaction(javax.transaction.UserTransaction) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection)

Example 10 with Account

use of org.jpox.samples.models.company.Account in project tests by datanucleus.

the class GeneralTest method testDelayedFlushExceptionJTA.

public void testDelayedFlushExceptionJTA() throws Exception {
    try {
        UserTransaction ut = getUserTransaction();
        ut.setTransactionTimeout(300);
        int totals = 0;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            ut.begin();
            Query q = pm.newQuery(Account.class);
            Collection c = (Collection) q.execute();
            totals = c.size();
            q.closeAll();
            ut.commit();
        } finally {
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(true);
        boolean rbk = false;
        try {
            ut.begin();
            Account accnt = new Account();
            accnt.setUsername(null);
            pm.makePersistent(accnt);
            ut.commit();
        }// e.g. JBoss wraps the NucleusDataStoreException
         catch (Exception e) {
            // Expected
            if (isRollbackDueToDatastoreException(e)) {
                rbk = true;
            }
        } finally {
            Assert.assertTrue("Not Rolledback", rbk);
            Assert.assertTrue("UserTransaction should not be active anymore?!", ut.getStatus() == Status.STATUS_NO_TRANSACTION);
            if (pm.currentTransaction().isActive() && ut.getClass().getName().startsWith("org.objectweb.jotm")) {
                // see http://www.jpox.org/servlet/jira/browse/NUCCORE-224
                fail("JOTM bug: when an exception is thrown during Synchronization.beforeCompletion(), the UserTransaction's status is " + "STATUS_NO_TRANSCTION, but there was no callback to Synchronization.afterCompletion()");
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            ut.begin();
            Query q = pm.newQuery(Account.class);
            Collection c = (Collection) q.execute();
            try {
                Assert.assertTrue(c.size() == (totals));
            } finally {
                q.closeAll();
                ut.commit();
            }
        } finally {
            pm.close();
        }
    } finally {
        clean(Account.class);
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Account(org.jpox.samples.models.company.Account) UserTransaction(javax.transaction.UserTransaction) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) NamingException(javax.naming.NamingException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)14 Account (org.jpox.samples.models.company.Account)14 LoginAccount (org.jpox.samples.one_one.unidir.LoginAccount)14 Transaction (javax.jdo.Transaction)12 UserTransaction (javax.transaction.UserTransaction)8 Collection (java.util.Collection)7 Query (javax.jdo.Query)7 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)5 JDOUserException (javax.jdo.JDOUserException)5 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)5 Employee (org.jpox.samples.models.company.Employee)5 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)4 Iterator (java.util.Iterator)3 Manager (org.jpox.samples.models.company.Manager)3 Set (java.util.Set)2 NamingException (javax.naming.NamingException)2 HeuristicMixedException (javax.transaction.HeuristicMixedException)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 NotSupportedException (javax.transaction.NotSupportedException)2 RollbackException (javax.transaction.RollbackException)2