Search in sources :

Example 6 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project motech by motech.

the class BaseInstanceIT method setUpEntity.

private void setUpEntity() throws Exception {
    String entityClass = getEntityClassName();
    entity = new EntityDto(entityClass);
    entity.setRecordHistory(true);
    entity = entityService.createEntity(entity);
    entityService.addFields(entity, getEntityFields());
    TrackingDto tracking = entityService.getAdvancedSettings(entity.getId(), true).getTracking();
    tracking.setAllowCreateEvent(false);
    tracking.setAllowUpdateEvent(false);
    tracking.setAllowDeleteEvent(false);
    entityService.updateTracking(entity.getId(), tracking);
    SchemaHolder schemaHolder = entityService.getSchema();
    mdsConstructor.constructEntities(schemaHolder);
    PersistenceManagerFactory factory = getDataPersistenceManagerFactory();
    if (null == factory.getMetadata(entityClass)) {
        factory.registerMetadata(metadataHolder.getJdoMetadata());
    }
    CtClass ctClass = MotechClassPool.getDefault().get(getRepositoryClass());
    MDSClassLoader.getInstance().safeDefineClass(getRepositoryClass(), ctClass.toBytecode());
    ctClass = MotechClassPool.getDefault().get(getInterfaceClass());
    MDSClassLoader.getInstance().safeDefineClass(getInterfaceClass(), ctClass.toBytecode());
    ctClass = MotechClassPool.getDefault().get(getServiceClass());
    MDSClassLoader.getInstance().safeDefineClass(getServiceClass(), ctClass.toBytecode());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) CtClass(javassist.CtClass) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) TrackingDto(org.motechproject.mds.dto.TrackingDto)

Example 7 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class SchemaColumnTest method testColumnDefaultsStoringNullWhenNull.

/**
 * Test of default values for columns, storing null when a field is null at persist.
 */
public void testColumnDefaultsStoringNullWhenNull() {
    Properties props = new Properties();
    props.setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_COLUMN_DEFAULT_WHEN_NULL, "false");
    PersistenceManagerFactory myPMF = getConfigurablePMF(1, props);
    try {
        PersistenceManager pm = myPMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = new ClassWithDefaultCols(1);
            pm.makePersistent(c1);
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        myPMF.getDataStoreCache().evictAll();
        // Retrieve and check data
        pm = myPMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = pm.getObjectById(ClassWithDefaultCols.class, 1);
            assertNull(c1.getDefaultedNameNull());
            assertNull(c1.getDefaultedName());
            assertNull(c1.getDefaultedLong());
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(myPMF, ClassWithDefaultCols.class);
        myPMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ClassWithDefaultCols(org.datanucleus.samples.schema.ClassWithDefaultCols) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties)

Example 8 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory 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)

Example 9 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class AttachDetachReplicateTest method testReplicateRelation_1to1_circular.

/**
 * This test creates a circular object-chain with unidirectional 1-1-relation (A.B.C.A,
 * where second A is the same as first A). After persisting it into datastore1,
 * it detaches A with FetchPlan.ALL and persists it into datastore2 using makePersistent.
 */
public void testReplicateRelation_1to1_circular() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        Transaction tx1 = null;
        Transaction tx2 = null;
        // Persist the object A with two 1-1-relations (A-B-C) into the first datastore.
        PersistenceManager pm1 = pmf.getPersistenceManager();
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            A a = new A("a");
            B b = new B("b");
            C c = new C("c");
            a.setB(b);
            b.setC(c);
            pm1.makePersistent(a);
            a.getB().getC().setA(a);
            tx1.commit();
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Detach A with FetchPlan.ALL from datastore 1.
        A detachedA = null;
        pm1 = pmf.getPersistenceManager();
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            try {
                pm1.getFetchPlan().setGroup(FetchPlan.ALL);
                pm1.getFetchPlan().setMaxFetchDepth(2);
                // So we have fetch-depth allowing the detach of all objects
                pm1.getFetchPlan().addGroup("includingB");
                A a = (A) pm1.getExtent(A.class).iterator().next();
                detachedA = (A) pm1.detachCopy(a);
            } catch (Exception x) {
                LOG.error("Loading instance of A from datastore 1 or detaching it with FetchPlan.ALL failed!", x);
                fail("Loading instance of A from datastore 1 or detaching it with FetchPlan.ALL failed: " + x.getMessage());
            }
            tx1.commit();
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // check, whether A.b and A.b.c exist and are correct in detachedA
        try {
            if (!"a".equals(detachedA.getName()))
                fail("detachedA.name was corrupted somewhere after creation; either during makePersistent or detachCopy! Should be \"a\", but is \"" + detachedA.getName() + "\"!");
            if (!"b".equals(detachedA.getB().getName()))
                fail("detachedA.b.name was corrupted somewhere after creation; either during makePersistent or detachCopy! Should be \"b\", but is \"" + detachedA.getB().getName() + "\"!");
            if (!"c".equals(detachedA.getB().getC().getName()))
                fail("detachedA.b.c.name was corrupted somewhere after creation; either during makePersistent or detachCopy! Should be \"c\", but is \"" + detachedA.getB().getC().getName() + "\"!");
        } catch (Exception x) {
            LOG.error("Accessing object graph detached from datastore1 failed!", x);
            fail("Accessing object graph detached from datastore1 failed: " + x.getMessage());
        }
        // Store detachedA into datastore 2 using makePersistent (the object does NOT yet exist there and should be created)
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            try {
                pm2.makePersistent(detachedA);
            } catch (Exception x) {
                LOG.error("makePersistent with object detached from datastore1 failed on datastore2!", x);
                fail("makePersistent with object detached from datastore1 failed on datastore2: " + x.getMessage());
            }
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
        // check, whether A.b and A.b.c exist and have been stored correctly into datastore2.
        pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            try {
                A a = (A) pm2.getExtent(A.class).iterator().next();
                if (!"a".equals(a.getName()))
                    fail("a.name was corrupted during makePersistent on datastore2! Should be \"a\", but is \"" + a.getName() + "\"!");
                if (!"b".equals(a.getB().getName()))
                    fail("a.b.name was corrupted during makePersistent on datastore2! Should be \"b\", but is \"" + a.getB().getName() + "\"!");
                if (!"c".equals(a.getB().getC().getName()))
                    fail("a.b.c.name was corrupted during makePersistent on datastore2! Should be \"c\", but is \"" + a.getB().getC().getName() + "\"!");
            } catch (Exception x) {
                LOG.error("Accessing datastore2 failed!", x);
                fail("Accessing datastore2 failed: " + x.getMessage());
            }
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } finally {
        // Clean out our data
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            PersistenceManagerFactory pmf = pmfs[i];
            PersistenceManager pm = pmf.getPersistenceManager();
            Transaction tx = pm.currentTransaction();
            tx.begin();
            Extent ext = pm.getExtent(A.class, false);
            Iterator it = ext.iterator();
            while (it.hasNext()) {
                A a = (A) it.next();
                a.setB(null);
            }
            tx.commit();
            tx.begin();
            ext = pm.getExtent(B.class, false);
            it = ext.iterator();
            while (it.hasNext()) {
                B b = (B) it.next();
                b.setC(null);
            }
            tx.commit();
            tx.begin();
            ext = pm.getExtent(C.class, false);
            it = ext.iterator();
            while (it.hasNext()) {
                C c = (C) it.next();
                c.setA(null);
            }
            tx.commit();
            pm.close();
            clean(pmf, A.class);
            clean(pmf, B.class);
            clean(pmf, C.class);
        }
    }
}
Also used : A(org.datanucleus.samples.detach.fetchdepth.A) B(org.datanucleus.samples.detach.fetchdepth.B) C(org.datanucleus.samples.detach.fetchdepth.C) PESSIMISTIC(org.datanucleus.tests.annotations.TransactionMode.Mode.PESSIMISTIC) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException)

Example 10 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory 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

PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)67 PersistenceManager (javax.jdo.PersistenceManager)52 Transaction (javax.jdo.Transaction)44 Properties (java.util.Properties)40 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)34 Employee (org.jpox.samples.models.company.Employee)18 Query (javax.jdo.Query)15 JDOUserException (javax.jdo.JDOUserException)13 Manager (org.jpox.samples.models.company.Manager)13 Iterator (java.util.Iterator)12 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)11 Extent (javax.jdo.Extent)10 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)10 JDOFatalUserException (javax.jdo.JDOFatalUserException)9 Collection (java.util.Collection)8 JDOException (javax.jdo.JDOException)8 DataStoreCache (javax.jdo.datastore.DataStoreCache)8 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)8 Level2Cache (org.datanucleus.cache.Level2Cache)8 SQLException (java.sql.SQLException)7