コード例 #1
0
  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;
    }
  }
コード例 #2
0
  public void testTransactionFlush_2() throws Exception {
    String name = "testTransactionFlush_2_" + System.currentTimeMillis();
    TransactionExt tx = (TransactionExt) odmg.newTransaction();

    tx.begin();
    PersistenceBroker broker = tx.getBroker();

    ODMGZoo obj = new ODMGZoo();
    obj.setName(name);
    tx.lock(obj, Transaction.WRITE);
    tx.flush();
    // System.err.println("First flush call, insert new object");

    // PB to query
    Criteria crit = new Criteria();
    crit.addEqualTo("name", obj.getName());
    QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class, crit);
    // we flushed all objects, thus we should found object in DB/cache
    Iterator it = broker.getIteratorByQuery(query);
    assertTrue(it.hasNext());
    ODMGZoo other = (ODMGZoo) it.next();
    assertNotNull(other);
    assertEquals(obj.getZooId(), other.getZooId());
    assertEquals(obj.getName(), other.getName());
    assertFalse(it.hasNext());

    /** * Charles : Start ** */
    // Let's flush, change the name and flush again
    tx.flush();
    // System.err.println("Second flush call, nothing to do");
    obj.setName("updated_" + name);
    tx.flush();
    // System.err.println("Third flush call, update");
    OQLQuery q = odmg.newOQLQuery();
    q.create("select zoos from " + ODMGZoo.class.getName() + " where name like $1");
    q.bind("updated_" + name);

    // Redo the query - we should find the object again
    it = ((Collection) q.execute()).iterator();
    assertTrue(it.hasNext());
    other = (ODMGZoo) it.next();
    assertNotNull(other);
    assertEquals(obj.getZooId(), other.getZooId());
    assertEquals(obj.getName(), other.getName());
    assertFalse(it.hasNext());
    /** * Charles : End ** */

    // now we abort tx, so all flushed objects shouldn't be found
    // in further queries
    tx.abort();

    tx = (TransactionExt) odmg.newTransaction();
    tx.begin();
    broker = tx.getBroker();
    QueryByIdentity query2 = new QueryByIdentity(obj);
    Object result = broker.getObjectByQuery(query2);
    tx.commit();

    assertNull("We should not find objects from aborted tx", result);
  }
コード例 #3
0
 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;
 }
コード例 #4
0
  void makePersistent(PersistenceBroker pb, int txNumber) throws SQLException, LookupException {
    // store new objects
    if (newObjs != null) {
      for (Object obj : newObjs) {
        pb.store(obj, ObjectModificationDefaultImpl.INSERT);
      }
    }

    boolean foundOptimisticException = false;

    // update objects
    if (objsToStore != null) {
      for (Object obj : objsToStore) {
        try {
          pb.store(obj, ObjectModificationDefaultImpl.UPDATE);
        } catch (OptimisticLockException ole) {
          pb.removeFromCache(obj);
          foundOptimisticException = true;
        }
      }
    }

    if (foundOptimisticException) {
      throw new jvstm.CommitException();
    }

    // delete objects
    if (objsToDelete != null) {
      for (Object obj : objsToDelete) {
        pb.delete(obj);
      }
    }

    // write m-to-n tuples
    if (mToNTuples != null) {
      for (RelationTupleInfo info : mToNTuples.values()) {
        updateMtoNRelation(pb, info);
      }
    }

    // write change logs
    Connection conn = pb.serviceConnectionManager().getConnection();
    writeAttrChangeLogs(conn, txNumber);
  }
コード例 #5
0
  public void testTransactionFlush() throws Exception {
    String name = "testTransactionFlush_" + System.currentTimeMillis();
    TransactionExt tx = (TransactionExt) odmg.newTransaction();

    tx.begin();
    PersistenceBroker broker = tx.getBroker();
    ODMGZoo obj = new ODMGZoo();
    tx.lock(obj, Transaction.WRITE);
    obj.setName(name);

    tx.flush();

    Criteria crit = new Criteria();
    crit.addEqualTo("name", obj.getName());
    QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class, crit);
    // we flushed all objects, thus we should find object in DB/cache
    Iterator it = broker.getIteratorByQuery(query);
    assertTrue(it.hasNext());
    ODMGZoo other = (ODMGZoo) it.next();
    assertNotNull(other);
    assertEquals(obj.getZooId(), other.getZooId());
    assertEquals(obj.getName(), other.getName());
    assertFalse(it.hasNext());
    // now we abort tx, so all flushed objects shouldn't be found
    // in further queries
    tx.abort();

    tx = (TransactionExt) odmg.newTransaction();
    tx.begin();
    broker = tx.getBroker();
    QueryByIdentity query2 = new QueryByIdentity(obj);
    Object result = broker.getObjectByQuery(query2);
    tx.commit();

    assertNull("We should not find objects from aborted tx", result);
  }
コード例 #6
0
  // copied and adapted from OJB's MtoNBroker
  protected void updateMtoNRelation(PersistenceBroker pb, RelationTupleInfo tupleInfo) {
    DomainObject obj1 = tupleInfo.obj1;
    DomainObject obj2 = tupleInfo.obj2;

    ClassDescriptor cld1 = pb.getDescriptorRepository().getDescriptorFor(obj1.getClass());
    CollectionDescriptor cod = cld1.getCollectionDescriptorByName(tupleInfo.colNameOnObj1);
    if (cod == null) {
      // try the mapping on the other object
      cld1 = pb.getDescriptorRepository().getDescriptorFor(obj2.getClass());
      cod = cld1.getCollectionDescriptorByName(tupleInfo.colNameOnObj2);

      // switch objects
      obj1 = tupleInfo.obj2;
      obj2 = tupleInfo.obj1;
    }

    String[] oidColumns = new String[2];
    oidColumns[0] = cod.getFksToThisClass()[0];
    oidColumns[1] = cod.getFksToItemClass()[0];

    ValueContainer[] oidValues = new ValueContainer[2];
    oidValues[0] = new ValueContainer(obj1.getOid(), OID_JDBC_TYPE);
    oidValues[1] = new ValueContainer(obj2.getOid(), OID_JDBC_TYPE);

    String table = cod.getIndirectionTable();

    // always remove the tuple
    String sqlStmt = pb.serviceSqlGenerator().getDeleteMNStatement(table, oidColumns, null);
    pb.serviceJdbcAccess().executeUpdateSQL(sqlStmt, cld1, oidValues, null);

    // if it was not to remove but to add, then add it
    // this "delete-first, add-after" serves to ensure that we can add
    // multiple times
    // the same tuple to a relation and still have the Set semantics for the
    // relation.
    if (!tupleInfo.remove) {
      sqlStmt = pb.serviceSqlGenerator().getInsertMNStatement(table, oidColumns, EMPTY_ARRAY);
      pb.serviceJdbcAccess().executeUpdateSQL(sqlStmt, cld1, oidValues, null);
    }
  }
コード例 #7
0
 protected int getDBObjectCount(PersistenceBroker broker, Class target) throws Exception {
   Criteria c = new Criteria();
   Query q = new QueryByCriteria(target, c);
   int count = broker.getCount(q);
   return count;
 }
コード例 #8
0
  /** 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);
  }