protected ClassDescriptor selectClassDescriptor(Map row) throws PersistenceBrokerException { String[] key = new String[column.length]; for (int i = 0; i < column.length; i++) { key[i] = (String) row.get(column[i]); } Class clazz = null; if (key != null) { clazz = chooseClass(key); } if (clazz == null) { return getClassDescriptor(); } PersistenceBroker broker = null; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker(); ClassDescriptor result = broker.getClassDescriptor(clazz); broker.close(); if (result == null) { return getClassDescriptor(); } else { return result; } } catch (PersistenceBrokerException e) { broker.close(); throw e; } }
protected int getDBObjectCountWithNewPB(Class target) throws Exception { PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); Criteria c = new Criteria(); Query q = new QueryByCriteria(target, c); int count = broker.getCount(q); broker.close(); return count; }
/** Tests object count after a commited transaction */ public void testResultsAfterTransactionWithClearedCache() throws Exception { int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); Transaction tx = odmg.newTransaction(); tx.begin(); // store persistentStoreObjects( database, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5)); persistentStoreObjects( database, getNewProjects("testResultsAfterTransactionWithClearedCache", 3)); // store more storeObjects(tx, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5)); storeObjects(tx, getNewProjects("testResultsAfterTransactionWithClearedCache", 2)); tx.commit(); // ###### hack we clear cache of PB ######## PersistenceBroker tmp = PersistenceBrokerFactory.defaultPersistenceBroker(); tmp.clearCache(); tmp.close(); int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter); assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter); assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL); assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL); // we do twice odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class); projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class); // tx should be reusable tx.begin(); // store persistentStoreObjects( database, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5)); persistentStoreObjects( database, getNewProjects("testResultsAfterTransactionWithClearedCache", 3)); // store more storeObjects(tx, getNewODMGZoos("testResultsAfterTransactionWithClearedCache", 5)); storeObjects(tx, getNewProjects("testResultsAfterTransactionWithClearedCache", 2)); tx.commit(); // ###### hack we clear cache of PB ######## tmp = PersistenceBrokerFactory.defaultPersistenceBroker(); tmp.clearCache(); tmp.close(); odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class); projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class); odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class); projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class); assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfter); assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfter); assertEquals("Wrong number of odmgZoos found", (odmgZoosBefore + 10), odmgZoosAfterOQL); assertEquals("Wrong number of projects found", (projectsBefore + 5), projectsAfterOQL); }