Search in sources :

Example 1 with Account

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

the class GeneralTest method testCloseOnTxnCompletion.

public void testCloseOnTxnCompletion() throws Exception {
    try {
        PersistenceManager pm = null;
        pm = pmf.getPersistenceManager();
        new PersistenceManagerDisposer(pm);
        pm.currentTransaction().begin();
        Account accnt = new Account();
        accnt.setUsername("jpox");
        pm.makePersistent(accnt);
        Object oid = pm.getObjectId(accnt);
        pm.currentTransaction().commit();
        assertTrue("The PersistenceManager is still open", pm.isClosed());
        pm = pmf.getPersistenceManager();
        new PersistenceManagerDisposer(pm);
        pm.currentTransaction().begin();
        accnt = (Account) pm.getObjectById(oid);
        pm.deletePersistent(accnt);
        pm.currentTransaction().commit();
        assertTrue("The PersistenceManager is still open", pm.isClosed());
        pm = pmf.getPersistenceManager();
        new PersistenceManagerDisposer(pm);
        pm.currentTransaction().begin();
        accnt = new Account();
        accnt.setUsername("jpox");
        pm.makePersistent(accnt);
        pm.currentTransaction().rollback();
        assertTrue("The PersistenceManager is still open", pm.isClosed());
        UserTransaction ut = getUserTransaction();
        ut.begin();
        pm = pmf.getPersistenceManager();
        new PersistenceManagerDisposer(pm);
        accnt = new Account();
        accnt.setUsername("jpox");
        pm.makePersistent(accnt);
        oid = pm.getObjectId(accnt);
        ut.commit();
        assertTrue("The PersistenceManager is still open", pm.isClosed());
        ut.begin();
        pm = pmf.getPersistenceManager();
        new PersistenceManagerDisposer(pm);
        accnt = (Account) pm.getObjectById(oid);
        pm.deletePersistent(accnt);
        ut.commit();
        assertTrue("The PersistenceManager is still open", pm.isClosed());
        ut.begin();
        pm = pmf.getPersistenceManager();
        new PersistenceManagerDisposer(pm);
        accnt = new Account();
        accnt.setUsername("jpox");
        pm.makePersistent(accnt);
        oid = pm.getObjectId(accnt);
        ut.rollback();
        assertTrue("The PersistenceManager is still open", pm.isClosed());
    } finally {
        clean(Account.class);
    }
}
Also used : PersistenceManagerDisposer(org.datanucleus.tests.jta.util.PersistenceManagerDisposer) UserTransaction(javax.transaction.UserTransaction) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Account(org.jpox.samples.models.company.Account) PersistenceManager(javax.jdo.PersistenceManager)

Example 2 with Account

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

the class GeneralTest method testBasicJTA.

public void testBasicJTA() 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();
            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();
            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();
        }
        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 + 1));
            } 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 3 with Account

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

the class GeneralTest method testAddDeleteJTA.

public void testAddDeleteJTA() throws Exception {
    try {
        boolean opt = false;
        UserTransaction ut = getUserTransaction();
        ut.setTransactionTimeout(300);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(opt);
        ut.begin();
        Query q = pm.newQuery(Account.class);
        Collection c = (Collection) q.execute();
        c.size();
        q.closeAll();
        ut.commit();
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(opt);
        ut.begin();
        Account accnt = new Account();
        accnt.setUsername("jpox");
        pm.makePersistent(accnt);
        Object oid = pm.getObjectId(accnt);
        ut.commit();
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(opt);
        ut.begin();
        accnt = (Account) pm.getObjectById(oid);
        pm.deletePersistent(accnt);
        ut.commit();
        pm.close();
        pm = pmf.getPersistenceManager();
        pm.currentTransaction().setOptimistic(opt);
        ut.begin();
        try {
            accnt = (Account) pm.getObjectById(oid);
            System.err.println(accnt);
            Assert.assertTrue("accnt still in db:" + pm.getObjectId(accnt), false);
        } catch (javax.jdo.JDOObjectNotFoundException ex) {
        } finally {
            ut.commit();
            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 4 with Account

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

the class GeneralTest method testConnectionAccessDuringAfterCompletion.

/**
 * Verify accessibility of a previously acquired DB connection during Synchronization.afterCompletion()
 * (e.g. for application code that performs invalidation of custom caches in afterCompletion(),
 * such as cached results of computations based on persistent values)
 */
public void testConnectionAccessDuringAfterCompletion() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
    try {
        final PersistenceManager pm = pmf.getPersistenceManager();
        // prepare a persistent object to be found later on
        pm.currentTransaction().begin();
        Account accnt = new Account();
        accnt.setUsername("jpox");
        pm.makePersistent(accnt);
        pm.currentTransaction().commit();
        final boolean[] success = new boolean[1];
        // register a Synchronization object that accesses the DB during afterCompletion()
        pm.currentTransaction().setSynchronization(new Synchronization() {

            public void beforeCompletion() {
            }

            public void afterCompletion(int arg0) {
                // access a DB connection acquired previously during the transaction
                dbConnectionAccess(pm);
                success[0] = true;
            }
        });
        UserTransaction ut = getUserTransaction();
        ut.begin();
        // acquire a db connection for the ongoing transaction
        dbConnectionAccess(pm);
        ut.commit();
        assertTrue("accessing the DB connection during afterCompletion() wasn't successful", success[0]);
    } 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) PersistenceManager(javax.jdo.PersistenceManager) Synchronization(javax.transaction.Synchronization)

Example 5 with Account

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

the class GeneralTest method testAddDeleteJTANoBatch.

public void testAddDeleteJTANoBatch() throws Exception {
    PersistenceManagerFactory myPMF = null;
    try {
        boolean opt = false;
        UserTransaction ut = getUserTransaction();
        ut.setTransactionTimeout(300);
        // Create PMF with no batching
        Properties props = new Properties();
        props.put("datanucleus.rdbms.statementBatchLimit", "0");
        myPMF = getPMF(1, props);
        PersistenceManager pm = myPMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(opt);
        ut.begin();
        Query q = pm.newQuery(Account.class);
        Collection c = (Collection) q.execute();
        c.size();
        q.closeAll();
        ut.commit();
        pm.close();
        pm = myPMF.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(opt);
        ut.begin();
        Account accnt = new Account();
        accnt.setUsername("jpox");
        pm.makePersistent(accnt);
        Object oid = pm.getObjectId(accnt);
        ut.commit();
        pm.close();
        pm = myPMF.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(opt);
        ut.begin();
        accnt = (Account) pm.getObjectById(oid);
        pm.deletePersistent(accnt);
        ut.commit();
        pm.close();
        pm = myPMF.getPersistenceManager();
        pm.currentTransaction().setOptimistic(opt);
        ut.begin();
        try {
            accnt = (Account) pm.getObjectById(oid);
            System.err.println(accnt);
            Assert.assertTrue("accnt still in db:" + pm.getObjectId(accnt), false);
        } catch (javax.jdo.JDOObjectNotFoundException ex) {
        } finally {
            ut.commit();
            pm.close();
        }
    } finally {
        clean(myPMF, Account.class);
        myPMF.close();
    }
}
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) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties)

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