Search in sources :

Example 6 with LoginAccount

use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.

the class AttachDetachTest method testCopyOnAttachFalseMultipleDetach.

/**
 * Test for use of CopyOnAttach=false with a 1-1 unidir relation and where we detach more than 1 of an object
 * and try to attach them in the same txn.
 */
public void testCopyOnAttachFalseMultipleDetach() {
    try {
        LoginAccount detachedAcct1 = null;
        LoginAccount detachedAcct2 = null;
        // Persist the owner object
        PersistenceManager pm = newPM();
        pm.setCopyOnAttach(false);
        pm.getFetchPlan().setGroup(FetchPlan.ALL).setMaxFetchDepth(2);
        Transaction tx = pm.currentTransaction();
        try {
            // Persist
            tx.begin();
            LoginAccount acct = new LoginAccount("George", "Bush", "bush", "iraq");
            pm.makePersistent(acct);
            // Detach 2 copies
            detachedAcct1 = (LoginAccount) pm.detachCopy(acct);
            detachedAcct2 = (LoginAccount) pm.detachCopy(acct);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Update the detached objects
        assertEquals("Account1 object is in incorrect state", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(detachedAcct1));
        assertEquals("Account2 object is in incorrect state", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(detachedAcct2));
        detachedAcct1.setFirstName("George W");
        detachedAcct2.setFirstName("George B");
        // Attach the detached objects
        pm = newPM();
        pm.setCopyOnAttach(false);
        tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(detachedAcct1);
            pm.flush();
            try {
                pm.makePersistent(detachedAcct2);
                LOG.error("Attach of second version of object succeeded!");
                fail("Attach of second version of an object succeeded but should have thrown exception since CopyOnAttach=false");
            } catch (JDOUserException ue) {
                // Expected so rethrow it
                throw ue;
            }
            tx.commit();
        } catch (Exception e) {
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(LoginAccount.class);
        clean(Login.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) JDOUserException(javax.jdo.JDOUserException) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 7 with LoginAccount

use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.

the class CacheTest method testRelationshipCachingWhenNontransactional.

public void testRelationshipCachingWhenNontransactional() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Create sample data
        Object acctId = null;
        try {
            tx.begin();
            LoginAccount acct = new LoginAccount("Barney", "Rubble", "brubble", "bambam");
            pm.makePersistent(acct);
            acctId = JDOHelper.getObjectId(acct);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while creating 1-1 unidirectional relationship data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Make sure the object isn't in the cache following persistence
        pmf.getDataStoreCache().evictAll();
        pmf.getDataStoreCache().pinAll(true, LoginAccount.class);
        pmf.getDataStoreCache().pinAll(true, Login.class);
        // Retrieve the record and check the data
        pm = pmf.getPersistenceManager();
        try {
            LOG.info(">> CacheTest fetching LoginAccount(1)");
            LoginAccount acct = (LoginAccount) pm.getObjectById(acctId);
            assertTrue("LoginAccount \"firstName\" is incorrect", acct.getFirstName().equals("Barney"));
            assertTrue("LoginAccount \"lastName\" is incorrect", acct.getLastName().equals("Rubble"));
            LOG.info(">> CacheTest fetching Login(1)");
            // Access the login field to make sure it is read. Should get L2 cached now
            Login login = acct.getLogin();
            assertEquals("Login \"login\" is incorrect", login.getUserName(), "brubble");
            assertEquals("Login \"password\" is incorrect", login.getPassword(), "bambam");
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while interrogating 1-1 unidirectional relationship data : " + e.getMessage());
        } finally {
            pm.close();
        }
        // login account is in the cache now after the last getObjectById
        LOG.info(">> CacheTest fetching LoginAccount(non-tx)");
        CachedPC cpc = ((JDOPersistenceManagerFactory) pmf).getNucleusContext().getLevel2Cache().get(acctId);
        assertTrue("LoginAccount is not cached", cpc != null);
        assertTrue("LoginAccount \"login\" is not cached", cpc.getFieldValue(2) != null);
        // Retrieve the record and check the data
        pm = pmf.getPersistenceManager();
        try {
            LoginAccount acct = (LoginAccount) pm.getObjectById(acctId);
            String[] loadedFields = NucleusJDOHelper.getLoadedFields(acct, pm);
            LOG.info(">> loadedFields=" + StringUtils.objectArrayToString(loadedFields));
            LOG.info(">> Accessing field login");
            Login login = acct.getLogin();
            LOG.info(">> login=" + login);
        } finally {
            pm.close();
        }
    } finally {
        clean(LoginAccount.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) CachedPC(org.datanucleus.cache.CachedPC) Login(org.jpox.samples.one_one.unidir.Login) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 8 with LoginAccount

use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testPersistOneToOneUni.

/**
 * Test of persist of 1-1 UNIDIR relation (PC field).
 */
public void testPersistOneToOneUni() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object acctId = null;
        Object loginId = null;
        try {
            tx.begin();
            LoginAccount acct = new LoginAccount("Mickey", "Mouse", "mickeym", "minnie");
            acct.setId(1);
            Login login = acct.getLogin();
            login.setId(1);
            pm.makePersistent(acct);
            tx.commit();
            acctId = pm.getObjectId(acct);
            loginId = pm.getObjectId(login);
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check data
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            LoginAccount acct = (LoginAccount) pm.getObjectById(acctId);
            Login login = (Login) pm.getObjectById(loginId);
            assertEquals("Acct first name retrieved is incorrect", "Mickey", acct.getFirstName());
            assertEquals("Acct last name retrieved is incorrect", "Mouse", acct.getLastName());
            assertEquals("Login username retrieved is incorrect", "mickeym", login.getUserName());
            assertEquals("Login password retrieved is incorrect", "minnie", login.getPassword());
            assertEquals("Login of LoginAccount retrieved is incorrect", login.getId(), acct.getLogin().getId());
            assertEquals("Login of LoginAccount retrieved is incorrect", login.getUserName(), acct.getLogin().getUserName());
            assertEquals("Login of LoginAccount retrieved is incorrect", login.getPassword(), acct.getLogin().getPassword());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown retrieving data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out data
        clean(LoginAccount.class);
        clean(Login.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Login(org.jpox.samples.one_one.unidir.Login) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 9 with LoginAccount

use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.

the class ManagedConnectionTest method testNontransactionalReleaseAfterUseFalse.

/**
 * Test case for managed connections in nontx usage where not releasing.
 */
public void testNontransactionalReleaseAfterUseFalse() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CONNECTION_NONTX_RELEASE_AFTER_USE, "false");
    PersistenceManagerFactory thePMF = getPMF(1, userProps);
    try {
        PersistenceManager pm = thePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Create sample data
        Object acctId = null;
        try {
            tx.begin();
            LoginAccount acct = new LoginAccount("Barney", "Rubble", "brubble", "bambam");
            pm.makePersistent(acct);
            acctId = JDOHelper.getObjectId(acct);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception while creating data", e);
            fail("Exception thrown while creating data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        try {
            for (int i = 0; i < 60; i++) {
                pm = thePMF.getPersistenceManager();
                pm.getObjectById(acctId);
                pm.close();
            }
        } catch (Exception e) {
            LOG.error("Exception while getting connections", e);
            fail("Exception thrown while getting connections : " + e.getMessage());
        } finally {
            if (!pm.isClosed()) {
                pm.close();
            }
        }
    } finally {
        thePMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties)

Example 10 with LoginAccount

use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.

the class ExtentTest method testRequiresExtent.

/**
 * Test the use of "requires-extent" being false.
 */
public void testRequiresExtent() throws Exception {
    try {
        // Create some objects.
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            LoginAccount acct = new LoginAccount("Andy", "Jefferson", "andy", "pwd");
            pm.makePersistent(acct);
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create of objects including one with requires-extent=false", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Try to get extent of "Login"
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.getExtent(Login.class, true);
            tx.commit();
            assertTrue("Should have thrown JDOUserException when getting Extent for class which has no extent defined.", false);
        } catch (JDOUserException ue) {
            LOG.info(ue);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out created data
        clean(LoginAccount.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) JDOUserException(javax.jdo.JDOUserException)

Aggregations

LoginAccount (org.jpox.samples.one_one.unidir.LoginAccount)12 PersistenceManager (javax.jdo.PersistenceManager)11 Transaction (javax.jdo.Transaction)10 Login (org.jpox.samples.one_one.unidir.Login)8 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)5 JDOUserException (javax.jdo.JDOUserException)3 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)2 JDOException (javax.jdo.JDOException)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 SystemException (javax.transaction.SystemException)2 UserTransaction (javax.transaction.UserTransaction)2 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)2 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)2 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1