Search in sources :

Example 81 with PersistenceManager

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

the class ApplicationIdPersistenceTest method testOptimisticVersionChecks.

/**
 * Test optimistic checking of surrogate version.
 */
public void testOptimisticVersionChecks() throws Exception {
    try {
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation o = new Organisation("DataNucleus");
            o.setDescription("The company behind this software");
            pm.makePersistent(o);
            tx.commit();
            id = pm.getObjectId(o);
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        tx1.begin();
        Organisation o1 = (Organisation) pm1.getObjectById(id);
        PersistenceManager pm2 = pmf.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        tx2.begin();
        Organisation o2 = (Organisation) pm2.getObjectById(id);
        // Update o1 in tx1 and commit it
        try {
            o1.setDescription("Global dataservices company");
            tx1.commit();
        } catch (Exception e) {
            LOG.error("Exception during retrieve/update in tx1", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Update o2 in tx2 and (try to) commit it
        try {
            o2.setDescription("Global dataservices company number 2");
            tx2.commit();
            fail("Should have thrown JDOOptimisticVerificationException!");
        } catch (Exception e) {
            if (e instanceof JDOOptimisticVerificationException) {
            // Expected
            } else {
                LOG.error("Incorrect exception during update in tx2", e);
                fail("Incorrect exception thrown when running test " + e.getMessage());
            }
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } finally {
        clean(Organisation.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 82 with PersistenceManager

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

the class ApplicationIdPersistenceTest method testOneToOne.

public void testOneToOne() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object p1Id = null;
        Object p2Id = null;
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            p2.setBestFriend(p1);
            pm.makePersistent(p2);
            tx.commit();
            p1Id = pm.getObjectId(p1);
            p2Id = pm.getObjectId(p2);
        } catch (Exception e) {
            LOG.error("Exception during 1-1 persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // No L2 cache interference
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Check number of objects present
            Query q = pm.newQuery(Person.class);
            List<Person> results = (List<Person>) q.execute();
            assertEquals(2, results.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-1 retrieve and check", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = null;
            try {
                p1 = (Person) pm.getObjectById(p1Id);
                assertEquals("Bugs", p1.getFirstName());
                assertEquals("Bunny", p1.getLastName());
                assertEquals(1, p1.getPersonNum());
            } catch (JDOObjectNotFoundException onfe) {
                fail("Person p1 not found");
            }
            assertNull("p1 best friend should be null", p1.getBestFriend());
            Person p2 = null;
            try {
                p2 = (Person) pm.getObjectById(p2Id);
                assertEquals("Daffy", p2.getFirstName());
                assertEquals("Duck", p2.getLastName());
                assertEquals(2, p2.getPersonNum());
            } catch (JDOObjectNotFoundException onfe) {
                fail("Person p2 not found");
            }
            assertNotNull("p2 best friend should not be null", p2.getBestFriend());
            assertEquals("p2 best friend should be same as p1 object", p1, p2.getBestFriend());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-1 retrieve and check", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 83 with PersistenceManager

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

the class ApplicationIdPersistenceTest method testEnum.

/**
 * Test persistence of an enum as a String and as ordinal.
 */
public void testEnum() {
    Palette p;
    Object id = null;
    try {
        // ---------------------
        // RED
        // ---------------------
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(100);
            p.setColour(Colour.RED);
            p.setColourOrdinal(Colour.RED);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(100, p.getAmount());
            assertEquals(Colour.RED, p.getColour());
            assertEquals(Colour.RED, p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // ---------------------
        // null
        // ---------------------
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(101);
            p.setColour(null);
            p.setColourOrdinal(null);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(101, p.getAmount());
            assertNull(p.getColour());
            assertNull(p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // ---------------------
        // GREEN
        // ---------------------
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = new Palette();
            p.setAmount(102);
            p.setColour(Colour.GREEN);
            p.setColourOrdinal(Colour.GREEN);
            pm.makePersistent(p);
            id = JDOHelper.getObjectId(p);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            p = (Palette) pm.getObjectById(id, true);
            assertEquals(102, p.getAmount());
            assertEquals(Colour.GREEN, p.getColour());
            assertEquals(Colour.GREEN, p.getColourOrdinal());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Palette.class);
    }
}
Also used : Palette(org.jpox.samples.types.enums.Palette) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 84 with PersistenceManager

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

the class ApplicationIdPersistenceTest method testSurrogateVersion.

/**
 * Test of persistence/retrieve of objects with surrogate version.
 */
public void testSurrogateVersion() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id = null;
        try {
            tx.begin();
            Organisation org1 = new Organisation("First");
            org1.setDescription("Original Description");
            pm.makePersistent(org1);
            tx.commit();
            id = JDOHelper.getObjectId(org1);
            assertEquals("Incorrect version after persist", new Long(1), JDOHelper.getVersion(org1));
        } catch (Exception e) {
            LOG.error("Exception persisting data", e);
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation org1 = (Organisation) pm.getObjectById(id);
            assertEquals("Incorrect version after getObjectById", new Long(1), JDOHelper.getVersion(org1));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception retrieving data", e);
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Organisation.class);
            List<Organisation> results = (List<Organisation>) q.execute();
            assertNotNull("No results from query!", results);
            assertEquals("Incorrect number of Organisation objects!", 1, results.size());
            Organisation org1 = results.iterator().next();
            assertEquals("Incorrect version after query", new Long(1), JDOHelper.getVersion(org1));
            // Provoke an update
            org1.setDescription("New Description");
            tx.commit();
            assertEquals("Incorrect version after update", new Long(2), JDOHelper.getVersion(org1));
        } catch (Exception e) {
            LOG.error("Exception retrieving data", e);
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation org1 = (Organisation) pm.getObjectById(id);
            assertEquals("Incorrect version after getObjectById", new Long(2), JDOHelper.getVersion(org1));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception retrieving data", e);
            fail("Exception thrown persisting data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Do clean up
        clean(Organisation.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 85 with PersistenceManager

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

the class ApplicationIdPersistenceTest method testInheritanceAndFloat.

/**
 * Test persistence of inheritance, and persistence of float.
 */
public void testInheritanceAndFloat() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee();
            e.setPersonNum(1);
            e.setGlobalNum("1");
            e.setFirstName("Bugs");
            e.setLastName("Bunny");
            e.setSalary(123);
            pm.makePersistent(e);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Employee.class);
            List<Employee> employees = (List<Employee>) q.execute();
            assertNotNull(employees);
            assertEquals("Number of employees is wrong", 1, employees.size());
            Employee e = employees.get(0);
            assertEquals("Employee name is wrong", "Bugs", e.getFirstName());
            assertEquals("Employee name is wrong", "Bunny", e.getLastName());
            assertEquals("Employee salary is wrong", 123, e.getSalary(), 0.1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1664 Transaction (javax.jdo.Transaction)1542 Query (javax.jdo.Query)671 List (java.util.List)397 JDOUserException (javax.jdo.JDOUserException)352 Collection (java.util.Collection)309 Iterator (java.util.Iterator)239 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)185 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)170 Person (org.jpox.samples.models.company.Person)146 Employee (org.jpox.samples.models.company.Employee)128 Manager (org.jpox.samples.models.company.Manager)107 HashSet (java.util.HashSet)101 ArrayList (java.util.ArrayList)85 SQLException (java.sql.SQLException)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)77 JDOException (javax.jdo.JDOException)74 Extent (javax.jdo.Extent)72 JDOFatalUserException (javax.jdo.JDOFatalUserException)68 JDODataStoreException (javax.jdo.JDODataStoreException)65