示例#1
0
 protected ODMGZoo newODMGZoo(String name) {
   ODMGZoo odmgZoo = new ODMGZoo();
   ++counter;
   if (name == null) {
     odmgZoo.setName("animal_" + counter);
   } else {
     odmgZoo.setName(name + "_" + counter);
   }
   return odmgZoo;
 }
示例#2
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);
  }
示例#3
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);
  }