Search in sources :

Example 71 with Transaction

use of javax.jdo.Transaction 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 72 with Transaction

use of javax.jdo.Transaction 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 73 with Transaction

use of javax.jdo.Transaction 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 74 with Transaction

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

Example 75 with Transaction

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

the class ApplicationIdPersistenceTest method testAccessJdoConnection.

public void testAccessJdoConnection() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = new Person();
            p.setPersonNum(1);
            p.setGlobalNum("1");
            p.setFirstName("Bugs");
            p.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("My");
            p2.setLastName("Friend");
            pm.makePersistent(p);
            pm.makePersistent(p2);
            JDOConnection conn = pm.getDataStoreConnection();
            assertTrue("Native connection should be instanceof com.mongodb.DB!", conn.getNativeConnection() instanceof DB);
            conn.close();
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist with connection access", 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) PersistenceManager(javax.jdo.PersistenceManager) JDOConnection(javax.jdo.datastore.JDOConnection) Person(org.jpox.samples.models.company.Person) DB(com.mongodb.DB) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

Transaction (javax.jdo.Transaction)1552 PersistenceManager (javax.jdo.PersistenceManager)1542 Query (javax.jdo.Query)635 List (java.util.List)395 JDOUserException (javax.jdo.JDOUserException)347 Collection (java.util.Collection)287 Iterator (java.util.Iterator)216 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)183 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)156 Person (org.jpox.samples.models.company.Person)144 Employee (org.jpox.samples.models.company.Employee)122 Manager (org.jpox.samples.models.company.Manager)98 HashSet (java.util.HashSet)97 SQLException (java.sql.SQLException)80 ArrayList (java.util.ArrayList)77 JDOException (javax.jdo.JDOException)73 Extent (javax.jdo.Extent)70 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)68 JDODataStoreException (javax.jdo.JDODataStoreException)65 JDOFatalUserException (javax.jdo.JDOFatalUserException)65