Search in sources :

Example 46 with PersistenceManagerFactory

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

the class PersistenceManagerFactoryTest method testServerTimeZoneID.

/**
 * Test for serverTimeZoneID setting.
 */
public void testServerTimeZoneID() {
    PersistenceManagerFactory pmf = null;
    // Try setting the serverTimeZoneID to an invalid value
    try {
        Properties userProps = new Properties();
        userProps.setProperty("javax.jdo.option.ServerTimeZoneID", "JPOX_CENTRAL_TIMEZONE");
        pmf = getPMF(1, userProps);
        fail("Expected a JDOUserException when setting the ServerTimeZoneID to an invalid value but worked!");
    } catch (JDOFatalUserException jdoe) {
    // Expected
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
    // Try setting it to a valid value and make sure it is retained
    try {
        Properties userProps = new Properties();
        userProps.setProperty("javax.jdo.option.ServerTimeZoneID", "UTC");
        pmf = getPMF(1, userProps);
        assertEquals("ServerTimeZoneID was not set correctly", ((JDOPersistenceManagerFactory) pmf).getServerTimeZoneID(), "UTC");
    } catch (Exception e) {
        fail("Exception thrown when setting the ServerTimeZoneID to UTC");
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
    // Try leaving it unset and check the value
    try {
        pmf = getPMF(1, null);
        assertEquals("ServerTimeZoneID was not set correctly", ((JDOPersistenceManagerFactory) pmf).getServerTimeZoneID(), null);
    } catch (Exception e) {
        fail("Exception thrown when setting the ServerTimeZoneID to UTC");
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
}
Also used : JDOFatalUserException(javax.jdo.JDOFatalUserException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException)

Example 47 with PersistenceManagerFactory

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

the class PersistenceManagerFactoryTest method testNullProperties.

/**
 * Test setting a null for for a property value but string was expected
 */
public void testNullProperties() {
    Properties props = TestHelper.getPropertiesForDatastore(1);
    Map<Object, Object> map = new HashMap<>();
    map.putAll(props);
    map.put("javax.jdo.option.Mapping", null);
    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(map);
    assertNotNull(pmf);
    pmf.close();
}
Also used : HashMap(java.util.HashMap) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties)

Example 48 with PersistenceManagerFactory

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

the class DynamicEnhanceSchemaToolTest method persist.

public void persist(DynamicEnhanceSchemaToolClassLoader runtimeCL) throws Exception {
    // Persist
    Map props = getPropertiesForDatastore(runtimeCL);
    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props);
    try {
        ClassLoaderResolver clr = ((JDOPersistenceManagerFactory) pmf).getNucleusContext().getClassLoaderResolver(runtimeCL);
        JDOMetadata filemd = pmf.newMetadata();
        createMetadata(filemd);
        NucleusLogger.PERSISTENCE.info(">> registering metadata");
        pmf.registerMetadata(filemd);
        NucleusLogger.PERSISTENCE.info(filemd);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id = null;
        try {
            tx.begin();
            Class clazz = clr.classForName("test.Client");
            Object o = clazz.newInstance();
            pm.makePersistent(o);
            tx.commit();
            id = pm.getObjectId(o);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Object obj = pm.getObjectById(id);
            pm.deletePersistent(obj);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        pmf.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOMetadata(javax.jdo.metadata.JDOMetadata) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 49 with PersistenceManagerFactory

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

the class MultithreadPMTest method testEvictAllAndWrites.

/**
 * Test changing the state
 */
public void testEvictAllAndWrites() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        Thread[] threads2 = new Thread[THREAD_SIZE];
        final PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        final Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        woody.setManager(bart);
        pm.makePersistent(woody);
        final Object id = pm.getObjectId(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        pm.getObjectById(id, true);
                        woody.setLastName("name");
                        woody.setManager(boss);
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads2[i] = new Thread(new Runnable() {

                    public void run() {
                        pm.evictAll();
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
                threads2[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                    threads2[i].join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

Example 50 with PersistenceManagerFactory

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

the class MultithreadPMTest method testMultipleTransitionRead.

/**
 * Test changing the state
 */
public void testMultipleTransitionRead() {
    Properties multiProps = new Properties();
    multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
    PersistenceManagerFactory myPMF = getPMF(1, multiProps);
    try {
        int THREAD_SIZE = 1000;
        Thread[] threads = new Thread[THREAD_SIZE];
        PersistenceManager pm = myPMF.getPersistenceManager();
        pm.currentTransaction().begin();
        final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        woody.setManager(bart);
        pm.makePersistent(woody);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        woody.getLastName();
                        woody.getManager().getLastName();
                    }
                });
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(myPMF);
        myPMF.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager)

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