Search in sources :

Example 26 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class MyApp method defineDynamicPersistentUnit.

public static void defineDynamicPersistentUnit() {
    PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
    pumd.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myfile_dynamicPMF.xml");
    pumd.addProperty("datanucleus.schema.autoCreateAll", "true");
    pumd.addProperty("datanucleus.xml.indentSize", "4");
    pmf = new JDOPersistenceManagerFactory(pumd, null);
    pm = pmf.getPersistenceManager();
}
Also used : JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData)

Example 27 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDOIntegrationTest method givenProduct_WhenQueryThenExist.

@Test
public void givenProduct_WhenQueryThenExist() {
    PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
    pumd.addClassName("com.baeldung.jdo.Product");
    pumd.setExcludeUnlistedClasses();
    pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
    pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
    pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
    pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
    pumd.addProperty("datanucleus.autoCreateSchema", "true");
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Product product = new Product("Tablet", 80.0);
        pm.makePersistent(product);
        Product product2 = new Product("Phone", 20.0);
        pm.makePersistent(product2);
        Product product3 = new Product("Laptop", 200.0);
        pm.makePersistent(product3);
        tx.commit();
    } catch (Throwable thr) {
        fail("Failed test : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pmf.close();
    PersistenceManagerFactory pmf2 = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm2 = pmf2.getPersistenceManager();
    Transaction tx2 = pm2.currentTransaction();
    try {
        tx2.begin();
        @SuppressWarnings("rawtypes") Query q = pm2.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price == 200");
        @SuppressWarnings("unchecked") List<Product> products = (List<Product>) q.execute();
        for (Product p : products) {
            assertEquals("Laptop", p.name);
        }
        tx2.commit();
    } finally {
        if (tx2.isActive()) {
            tx2.rollback();
        }
        pm2.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) List(java.util.List) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) Test(org.junit.Test)

Example 28 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDOIntegrationTest method givenProduct_WhenNewThenPerformTransaction.

@Test
public void givenProduct_WhenNewThenPerformTransaction() {
    PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
    pumd.addClassName("com.baeldung.jdo.Product");
    pumd.setExcludeUnlistedClasses();
    pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
    pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
    pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
    pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
    pumd.addProperty("datanucleus.autoCreateSchema", "true");
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        for (int i = 0; i < 100; i++) {
            String nam = "Product-" + i;
            Product productx = new Product(nam, (double) i);
            pm.makePersistent(productx);
        }
        tx.commit();
    } catch (Throwable thr) {
        fail("Failed test : " + thr.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pmf.close();
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) Test(org.junit.Test)

Example 29 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.

the class PersistenceManagerFactoryTest method testSerialize.

public void testSerialize() {
    try {
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            pm.makePersistent(p);
            tx.commit();
            id = pm.getObjectId(p);
        } catch (Exception e) {
            LOG.error("Exception persisting object", e);
            fail("Exception persisting object : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Serialize the current PMF
        byte[] bytes = null;
        try {
            bytes = TestHelper.serializeObject(pmf);
        } catch (RuntimeException re) {
            LOG.error("Error serializing PMF", re);
            fail("Error in serialization : " + re.getMessage());
        }
        // Deserialise the PMF
        JDOPersistenceManagerFactory pmf = null;
        try {
            pmf = (JDOPersistenceManagerFactory) TestHelper.deserializeObject(bytes);
        } catch (RuntimeException re) {
            LOG.error("Error deserializing PMF", re);
            fail("Error in deserialization : " + re.getMessage());
        }
        assertNotNull(pmf);
        assertNotNull(pmf.getNucleusContext());
        // Retrieve the object using the deserialised PM(F)
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = (Person) pm.getObjectById(id);
            assertNotNull(p);
            assertEquals("Fred", p.getFirstName());
            assertEquals("Flintstone", p.getLastName());
            assertEquals("fred.flintstone@warnerbros.com", p.getEmailAddress());
            Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@warnerbros.com");
            pm.makePersistent(p2);
            pm.flush();
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception retrieving object with deserialised PMF", e);
            fail("Exception retrieving object with deserialised PMF: " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException)

Example 30 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory 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)

Aggregations

JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)39 PersistenceManager (javax.jdo.PersistenceManager)15 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)12 NucleusContext (org.datanucleus.NucleusContext)9 Query (javax.jdo.Query)8 MetaDataManager (org.datanucleus.metadata.MetaDataManager)7 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)7 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)6 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 JDOException (javax.jdo.JDOException)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 IOException (java.io.IOException)3 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3