Search in sources :

Example 31 with Extent

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

the class PersistenceManagerTest method testNontransactionalUpdate.

/**
 * Test for NontransactionalWrite updates.
 */
public void testNontransactionalUpdate() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Primitive p = null;
        try {
            BigDecimal bd = new BigDecimal("12345.12345");
            BigInteger bi = new BigInteger("12345");
            java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
            java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
            java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
            java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
            tx.begin();
            p = new Primitive();
            setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            pm.makePersistent(p);
            tx.setRetainValues(true);
            tx.commit();
            // Update the data without a transaction
            tx.setNontransactionalRead(true);
            tx.setNontransactionalWrite(true);
            p.setIntObject(new Integer(1));
            p.setShortObject(new Short((short) 2));
            p = null;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setNontransactionalRead(true);
        tx.setNontransactionalWrite(true);
        try {
            // Check read of persisted Primitive making sure that the previous write was persisted ok
            Extent clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
            p = (Primitive) clnPrimitive.iterator().next();
            assertNotNull(p);
            assertEquals(1, p.getIntObject().intValue());
            assertEquals(2, p.getShortObject().shortValue());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Primitive.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) BigInteger(java.math.BigInteger)

Example 32 with Extent

use of javax.jdo.Extent in project datanucleus-api-jdo by datanucleus.

the class JDOReplicationManager method replicate.

/**
 * Method to perform the replication for all objects of the specified class names.
 * @param classNames Classes to replicate
 */
public void replicate(String... classNames) {
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("012052", pmfSource, pmfTarget, StringUtils.objectArrayToString(classNames)));
    }
    // Check if classes are detachable
    NucleusContext nucleusCtxSource = ((JDOPersistenceManagerFactory) pmfSource).getNucleusContext();
    MetaDataManager mmgr = nucleusCtxSource.getMetaDataManager();
    ClassLoaderResolver clr = nucleusCtxSource.getClassLoaderResolver(null);
    for (int i = 0; i < classNames.length; i++) {
        AbstractClassMetaData cmd = mmgr.getMetaDataForClass(classNames[i], clr);
        if (!cmd.isDetachable()) {
            throw new JDOUserException("Class " + classNames[i] + " is not detachable so cannot replicate");
        }
    }
    Object[] detachedObjects = null;
    // Detach from datastore 1
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("012053"));
    }
    PersistenceManager pm1 = pmfSource.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    if (getBooleanProperty("datanucleus.replicateObjectGraph")) {
        pm1.getFetchPlan().setGroup(javax.jdo.FetchPlan.ALL);
        pm1.getFetchPlan().setMaxFetchDepth(-1);
    }
    try {
        tx1.begin();
        clr = ((JDOPersistenceManager) pm1).getExecutionContext().getClassLoaderResolver();
        ArrayList objects = new ArrayList();
        for (int i = 0; i < classNames.length; i++) {
            Class cls = clr.classForName(classNames[i]);
            AbstractClassMetaData cmd = mmgr.getMetaDataForClass(cls, clr);
            if (!cmd.isEmbeddedOnly()) {
                Extent ex = pm1.getExtent(cls);
                Iterator iter = ex.iterator();
                while (iter.hasNext()) {
                    objects.add(iter.next());
                }
            }
        }
        Collection detachedColl = pm1.detachCopyAll(objects);
        detachedObjects = detachedColl.toArray();
        tx1.commit();
    } finally {
        if (tx1.isActive()) {
            tx1.rollback();
        }
        pm1.close();
    }
    replicateInTarget(detachedObjects);
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JDOUserException(javax.jdo.JDOUserException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection)

Example 33 with Extent

use of javax.jdo.Extent in project datanucleus-api-jdo by datanucleus.

the class JDOReplicationManager method replicate.

/**
 * Method to perform the replication for all objects of the specified types.
 * @param types Classes to replicate
 */
public void replicate(Class... types) {
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("012052", pmfSource, pmfTarget, StringUtils.objectArrayToString(types)));
    }
    // Check if classes are detachable
    NucleusContext nucleusCtxSource = ((JDOPersistenceManagerFactory) pmfSource).getNucleusContext();
    MetaDataManager mmgr = nucleusCtxSource.getMetaDataManager();
    ClassLoaderResolver clr = nucleusCtxSource.getClassLoaderResolver(null);
    for (int i = 0; i < types.length; i++) {
        AbstractClassMetaData cmd = mmgr.getMetaDataForClass(types[i], clr);
        if (!cmd.isDetachable()) {
            throw new JDOUserException("Class " + types[i] + " is not detachable so cannot replicate");
        }
    }
    Object[] detachedObjects = null;
    // Detach from datastore 1
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("012053"));
    }
    PersistenceManager pm1 = pmfSource.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    if (getBooleanProperty("datanucleus.replicateObjectGraph")) {
        pm1.getFetchPlan().setGroup(javax.jdo.FetchPlan.ALL);
        pm1.getFetchPlan().setMaxFetchDepth(-1);
    }
    try {
        tx1.begin();
        ArrayList objects = new ArrayList();
        for (int i = 0; i < types.length; i++) {
            AbstractClassMetaData cmd = mmgr.getMetaDataForClass(types[i], clr);
            if (!cmd.isEmbeddedOnly()) {
                Extent ex = pm1.getExtent(types[i]);
                Iterator iter = ex.iterator();
                while (iter.hasNext()) {
                    objects.add(iter.next());
                }
            }
        }
        Collection detachedColl = pm1.detachCopyAll(objects);
        detachedObjects = detachedColl.toArray();
        tx1.commit();
    } finally {
        if (tx1.isActive()) {
            tx1.rollback();
        }
        pm1.close();
    }
    replicateInTarget(detachedObjects);
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JDOUserException(javax.jdo.JDOUserException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection)

Example 34 with Extent

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

the class OptimisticTest method testOptimisticWithIdentityFieldObjects.

/**
 * Test of use of autoincrement ids with optimistic transactions.
 * Autoincrement ids are generated in the datastore and so will call flush() from the outset.
 */
public void testOptimisticWithIdentityFieldObjects() throws Exception {
    if (!storeMgr.supportsValueGenerationStrategy("identity")) {
        // Lets just say it passed
        return;
    }
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setOptimistic(true);
        try {
            tx.begin();
            Trade6Holder t6a = new Trade6Holder("NASDAQ 1");
            Trade6Holder t6b = new Trade6Holder("FTSE 2");
            Trade6Holder t6c = new Trade6Holder("CAC 5");
            pm.makePersistent(t6a);
            pm.makePersistent(t6b);
            pm.makePersistent(t6c);
            HashSet c = new HashSet();
            c.add(new Trade6("Donald Duck", 123.45, new Date()));
            c.add(new Trade6("Mickey Mouse", 234.5, new Date()));
            t6a.setTrades(c);
            c = new HashSet();
            c.add(new Trade6("Yogi Bear", 2300.0, new Date()));
            c.add(new Trade6("Minnie Mouse", 1.0, new Date()));
            t6b.setTrades(c);
            c = new HashSet();
            c.add(new Trade6("Barney Rubble", 1245.0, new Date()));
            c.add(new Trade6("Fred Flintstone", 2.0, new Date()));
            t6c.setTrades(c);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("Exception thrown during test of autoincrement with flushed objects");
            LOG.error(e);
            fail("Exception thrown during test of autoincrement with flushed objects : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ex = pm.getExtent(Trade6.class);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Trade6 tr6 = (Trade6) iter.next();
                if (tr6.getHolder() != null) {
                    Trade6Holder holder = tr6.getHolder();
                    tr6.setHolder(null);
                    holder.setTrades(null);
                }
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        clean(Trade6Holder.class);
        clean(Trade6.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Trade6(org.jpox.samples.versioned.Trade6) Trade6Holder(org.jpox.samples.versioned.Trade6Holder) Date(java.util.Date) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) HashSet(java.util.HashSet)

Example 35 with Extent

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

the class NondurableIdTest method testPersist.

/**
 * Method to test the persistence of nondurable objects.
 */
public void testPersist() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_NONDURABLE_ID)) {
        return;
    }
    try {
        // Persist some "nondurable" objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            List<LogEntry> entries = new ArrayList<>();
            LogEntry entry = new LogEntry(LogEntry.WARNING, "Datastore adapter not found. Falling back to default");
            entries.add(entry);
            entries.add(new LogEntry(LogEntry.ERROR, "No datastore specified"));
            entries.add(new LogEntry(LogEntry.INFO, "Object X1 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object X2 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object Y1 persisted"));
            entries.add(new LogEntry(LogEntry.ERROR, "Error persisting object Y2"));
            pm.makePersistentAll(entries);
            tx.commit();
            Object id = pm.getObjectId(entry);
            // Try to access LogEntry in HOLLOW state
            try {
                entry.getLevel();
                fail("Attempt to access field of HOLLOW nondurable instance succeeded!");
            } catch (JDOUserException e) {
            // Expected JDO2 [5.4.4] Access of field of HOLLOW nondurable throws JDOUserException
            }
            // Try to access the (HOLLOW) object using its id
            try {
                tx.begin();
                pm.getObjectById(id);
                fail("Attempt to access hollow nondurable instance succeeded!");
                tx.commit();
            } catch (JDOUserException e) {
            // Expected : JDO2 [5.4.4] Attempt to access nondurable object with id throws JDOUserException
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve some objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Try an Extent
            Extent ex = pm.getExtent(LogEntry.class);
            Iterator iter = ex.iterator();
            int noEntries = 0;
            while (iter.hasNext()) {
                Object obj = iter.next();
                Object id = JDOHelper.getObjectId(obj);
                assertNotNull("Identity of nondurable object retrieved from Extent is null!", id);
                noEntries++;
            }
            assertEquals("Number of LogEntry objects retrieved via Extent is incorrect", 6, noEntries);
            // Try a query for particular objects
            Query q = pm.newQuery("SELECT FROM " + LogEntry.class.getName() + " WHERE level == 1");
            List results = (List) q.execute();
            assertEquals("Number of LogEntry objects retrieved via Query (WARNING) is incorrect", 3, results.size());
            Iterator queryIter = results.iterator();
            while (queryIter.hasNext()) {
                Object obj = queryIter.next();
                Object id = JDOHelper.getObjectId(obj);
                assertNotNull("Identity of nondurable object retrieved from Query is null!", id);
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clear out our data
        clean(LogEntry.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) JDOUserException(javax.jdo.JDOUserException) LogEntry(org.datanucleus.samples.nondurable.LogEntry)

Aggregations

Extent (javax.jdo.Extent)72 PersistenceManager (javax.jdo.PersistenceManager)72 Transaction (javax.jdo.Transaction)70 Iterator (java.util.Iterator)62 JDOUserException (javax.jdo.JDOUserException)35 Collection (java.util.Collection)22 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)22 Query (javax.jdo.Query)15 Manager (org.jpox.samples.models.company.Manager)13 StoreManager (org.datanucleus.store.StoreManager)11 Method (java.lang.reflect.Method)10 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)10 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)10 Employee (org.jpox.samples.models.company.Employee)10 JDOException (javax.jdo.JDOException)9 SQLException (java.sql.SQLException)8 Department (org.jpox.samples.models.company.Department)8 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)7 Primitive (org.datanucleus.samples.widget.Primitive)6 List (java.util.List)5