Search in sources :

Example 1 with User1

use of org.datanucleus.samples.metadata.user.User1 in project tests by datanucleus.

the class CacheTest method testCommitFailWith2ndLevelCache.

public void testCommitFailWith2ndLevelCache() throws Exception {
    if (vendorID == null) {
        // Not applicable if not RDBMS
        return;
    }
    PersistenceManager pm = null;
    Transaction tx = null;
    try {
        pm = pmf.getPersistenceManager();
        User1 a = new User1();
        a.setId("NUCCORE-1204");
        a.setName("NUCCORE-1204");
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(a);
        pm.flush();
        JDOConnection con = pm.getDataStoreConnection();
        Connection o = (Connection) con.getNativeConnection();
        // emulate connection closing after flush but before commit
        o.close();
        con.close();
        try {
            tx.commit();
            fail("Commit should not succeed for closed connection: " + o);
        } catch (IllegalStateException e) {
            // commit succeeded - test failed
            throw e;
        } catch (Exception e) {
            // exception expected
            try {
                tx.rollback();
            } catch (Exception e2) {
            }
        }
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        User1 p = null;
        try {
            p = pm.getObjectById(User1.class, "NUCCORE-1204");
        } catch (JDOObjectNotFoundException e) {
            LOG.info("Exception caught", e);
        // ignore
        }
        if (p != null) {
            fail("Object was in 2nd level cache: " + p);
        }
        tx.commit();
        pm.close();
    } catch (Exception e) {
        LOG.error("Exception in test", e);
        fail("" + e);
    } finally {
        try {
            if (tx.isActive()) {
                tx.rollback();
            }
        } catch (Exception ignore) {
        }
        try {
            if (!pm.isClosed()) {
                pm.close();
            }
        } catch (Exception ignore) {
        }
        clean(User1.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager) User1(org.datanucleus.samples.metadata.user.User1) Connection(java.sql.Connection) JDOConnection(javax.jdo.datastore.JDOConnection) JDOConnection(javax.jdo.datastore.JDOConnection) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 2 with User1

use of org.datanucleus.samples.metadata.user.User1 in project tests by datanucleus.

the class BasicTest method testUseOfObjectIdClass.

public void testUseOfObjectIdClass() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            User1 u1 = new User1();
            u1.setId("u1");
            u1.setName("user1");
            UserGroup1 ug1 = new UserGroup1();
            ug1.setId("ug1");
            ug1.setName("userg1");
            ug1.setUserGroupName("usergn1");
            pm.makePersistent(ug1);
            pm.makePersistent(u1);
            User2 u2 = new User2();
            u2.setId("u2");
            u2.setName("user2");
            UserGroup2 ug2 = new UserGroup2();
            ug2.setId("ug2");
            ug2.setName("userg2");
            ug2.setUserGroupName("usergn2");
            pm.makePersistent(ug2);
            pm.makePersistent(u2);
            User3 u3 = new User3();
            u3.setId("u3");
            u3.setName("user3");
            UserGroup3 ug3 = new UserGroup3();
            ug3.setId("ug3");
            ug3.setName("userg3");
            ug3.setUserGroupName("usergn3");
            pm.makePersistent(ug3);
            pm.makePersistent(u3);
            tx.commit();
            tx.begin();
            u1 = (User1) pm.getObjectById(new UserId1("u1"), true);
            assertTrue(u1.getName().equals("user1"));
            ug1 = (UserGroup1) pm.getObjectById(new UserId1("ug1"), true);
            assertTrue(ug1.getName().equals("userg1"));
            u2 = (User2) pm.getObjectById(new UserId2("u2"), true);
            assertTrue(u2.getName().equals("user2"));
            ug2 = (UserGroup2) pm.getObjectById(new UserId2("ug2"), true);
            assertTrue(ug2.getName().equals("userg2"));
            u3 = (User3) pm.getObjectById(new UserId3("u3"), true);
            assertTrue(u3.getName().equals("user3"));
            ug3 = (UserGroup3) pm.getObjectById(new UserId3("ug3"), true);
            assertTrue(ug3.getName().equals("userg3"));
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(UserGroup1.class);
        clean(UserGroup2.class);
        clean(UserGroup3.class);
        clean(User1.class);
        clean(User2.class);
        clean(User3.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) User2(org.datanucleus.samples.metadata.user.User2) PersistenceManager(javax.jdo.PersistenceManager) User1(org.datanucleus.samples.metadata.user.User1) UserId1(org.datanucleus.samples.metadata.user.UserId1) User3(org.datanucleus.samples.metadata.user.User3) UserGroup1(org.datanucleus.samples.metadata.user.UserGroup1) UserGroup2(org.datanucleus.samples.metadata.user.UserGroup2) UserId3(org.datanucleus.samples.metadata.user.UserId3) UserGroup3(org.datanucleus.samples.metadata.user.UserGroup3) UserId2(org.datanucleus.samples.metadata.user.UserId2)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 User1 (org.datanucleus.samples.metadata.user.User1)2 Connection (java.sql.Connection)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOConnection (javax.jdo.datastore.JDOConnection)1 User2 (org.datanucleus.samples.metadata.user.User2)1 User3 (org.datanucleus.samples.metadata.user.User3)1 UserGroup1 (org.datanucleus.samples.metadata.user.UserGroup1)1 UserGroup2 (org.datanucleus.samples.metadata.user.UserGroup2)1 UserGroup3 (org.datanucleus.samples.metadata.user.UserGroup3)1 UserId1 (org.datanucleus.samples.metadata.user.UserId1)1 UserId2 (org.datanucleus.samples.metadata.user.UserId2)1 UserId3 (org.datanucleus.samples.metadata.user.UserId3)1