Search in sources :

Example 1 with JFireOrganisationID

use of org.datanucleus.samples.jfire.organisation.JFireOrganisationID in project tests by datanucleus.

the class AttachDetachReplicateTest method testReplicateSimple2.

/**
 * Another test of replication, with no relations. Just the object
 */
public void testReplicateSimple2() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        PersistenceManager pm1 = pmf.getPersistenceManager();
        JFireOrganisation organisation = null;
        JFireOrganisationID organisationID = JFireOrganisationID.create("datanucleus.jfire.org");
        // Persist in first DB
        Transaction tx = null;
        try {
            tx = pm1.currentTransaction();
            tx.begin();
            JFireOrganisation org1 = new JFireOrganisation(organisationID.organisationID);
            org1 = pm1.makePersistent(org1);
            // Detach it for copying
            organisation = pm1.detachCopy(org1);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown persisting/detaching object", ue);
            fail("Exception thrown while creating object in first datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm1.close();
        }
        // Check the detached object
        if (!JDOHelper.isDetached(organisation)) {
            fail("Organisation has not been detached!");
        }
        // Copy to other DB
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        tx = pm2.currentTransaction();
        try {
            tx.begin();
            pm2.makePersistent(organisation);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown replicating object", ue);
            fail("Exception thrown while copying object into second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the persistence in the second datastore
        try {
            tx = pm2.currentTransaction();
            tx.begin();
            // Use Extent since PM may have just put object in cache.
            Extent e = pm2.getExtent(JFireOrganisation.class);
            Iterator iter = e.iterator();
            boolean copied = false;
            while (iter.hasNext()) {
                JFireOrganisation o = (JFireOrganisation) iter.next();
                if (pm2.getObjectId(o).equals(organisationID)) {
                    copied = true;
                    break;
                }
            }
            assertTrue("Organisation was not copied to second datastore!", copied);
            tx.commit();
        } catch (JDOException ue) {
            LOG.error("Exception thrown checking results", ue);
            fail("Exception thrown while querying object in second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm2.close();
        }
    } finally {
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            clean(pmf, JFireOrganisation.class);
        }
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JFireOrganisation(org.datanucleus.samples.jfire.organisation.JFireOrganisation) Extent(javax.jdo.Extent) JFireOrganisationID(org.datanucleus.samples.jfire.organisation.JFireOrganisationID) Iterator(java.util.Iterator) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOException(javax.jdo.JDOException)

Aggregations

Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1 JDOException (javax.jdo.JDOException)1 PersistenceManager (javax.jdo.PersistenceManager)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 Transaction (javax.jdo.Transaction)1 JFireOrganisation (org.datanucleus.samples.jfire.organisation.JFireOrganisation)1 JFireOrganisationID (org.datanucleus.samples.jfire.organisation.JFireOrganisationID)1