Exemplo n.º 1
0
  public void testCheckCacheAfterRollback() throws Exception {
    RollbackObjectOne ro = null;
    RollbackObjectOne ro_2 = null;
    String name = "testCheckCacheAfterRollback_" + System.currentTimeMillis();

    Transaction tx = odmg.newTransaction();
    tx.begin();
    ro = new RollbackObjectOne();
    ro.setName(name);
    tx.lock(ro, Transaction.WRITE);

    ro_2 = new RollbackObjectOne();
    ro_2.setName(name);
    tx.lock(ro_2, Transaction.WRITE);
    tx.commit();

    tx = odmg.newTransaction();
    tx.begin();
    OQLQuery query = odmg.newOQLQuery();
    query.create("select all from " + RollbackObjectOne.class.getName() + " where name like $1");
    query.bind(name);
    List list = (List) query.execute();
    tx.commit();
    assertEquals(2, list.size());

    tx = odmg.newTransaction();
    tx.begin();
    tx.lock(ro, Transaction.WRITE);
    ro = new RollbackObjectOne();
    ro.setDescription(name);

    tx.lock(ro_2, Transaction.WRITE);
    ro_2 = new RollbackObjectOne();
    ro_2.setDescription(name);

    tx.abort();

    tx = odmg.newTransaction();
    tx.begin();
    query = odmg.newOQLQuery();
    query.create("select all from " + RollbackObjectOne.class.getName() + " where name like $1");
    query.bind(name);
    list = (List) query.execute();
    tx.commit();
    assertEquals(2, list.size());
    assertNull(((RollbackObjectOne) list.get(0)).getDescription());
    assertNull(((RollbackObjectOne) list.get(1)).getDescription());

    // after tx another tx should be able to lock these objects
    TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction();
    tx2.begin();
    try {
      Iterator it = list.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }
    } finally {
      tx2.abort();
    }
  }
Exemplo n.º 2
0
  /**
   * Tests behavior within transactions. If i store 5 odmgZoos within a transaction and after that
   * within the same transaction i do query 'select all odmgZoos' the number of odmgZoos returned
   * should be oldNumber+5 when using checkpoint. thma: this testcase seems to fail for some strange
   * problems with the testbed data the thrown error is unrelated to the things covered in the
   * testcase. arminw: should be fixed
   */
  public void testResultsWhileTransactionWithCheckpoint() throws Exception {
    // if(ojbSkipKnownIssueProblem()) return;

    int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
    int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);

    TransactionExt tx = (TransactionExt) odmg.newTransaction();
    tx.begin();
    List zoos = new ArrayList(getNewODMGZoos("testResultsWhileTransactionWithCheckpoint", 5));
    List projects = new ArrayList(getNewProjects("testResultsWhileTransactionWithCheckpoint", 3));
    // store some objects
    storeObjects(tx, zoos);
    storeObjects(tx, projects);
    // checkpoint, should bring objects to DB but shouldn't commit
    tx.checkpoint();

    // Do a queries within a transaction
    int odmgZoosWhile = getDBObjectCountViaOqlQuery(odmg, ODMGZoo.class);
    int projectsWhile = getDBObjectCountViaOqlQuery(odmg, ODMGGourmet.class);
    int odmgZoosWhilePB = 0;
    int projectsWhilePB = 0;

    odmgZoosWhilePB = getDBObjectCount(tx.getBroker(), ODMGZoo.class);
    projectsWhilePB = getDBObjectCount(tx.getBroker(), ODMGGourmet.class);

    // store more
    List zoos2 = new ArrayList(getNewODMGZoos("testResultsWhileTransactionWithCheckpoint", 5));
    List projects2 = new ArrayList(getNewProjects("testResultsWhileTransactionWithCheckpoint", 2));
    storeObjects(tx, zoos2);
    storeObjects(tx, projects2);

    zoos.addAll(zoos2);
    projects.addAll(projects2);
    // checkpoint, should bring objects to DB but shouldn't commit
    tx.checkpoint();

    // after checkpoint another tx should NOT be able to lock these objects
    TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction();
    tx2.begin();
    try {
      Iterator it = zoos.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }

      it = projects.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }
      fail("After checkpoint all locks should be still exist for objects");
    } catch (LockNotGrantedException e) {
      // expected
      assertTrue(true);
    } finally {
      tx2.abort();
    }
    // reassign tx
    tx.join();

    // more queries
    int odmgZoosWhile2 = getDBObjectCountViaOqlQuery(odmg, ODMGZoo.class);
    int projectsWhile2 = getDBObjectCountViaOqlQuery(odmg, ODMGGourmet.class);
    int odmgZoosWhilePB2 = 0;
    int projectsWhilePB2 = 0;

    odmgZoosWhilePB2 = getDBObjectCount(tx.getBroker(), ODMGZoo.class);
    projectsWhilePB2 = getDBObjectCount(tx.getBroker(), ODMGGourmet.class);

    tx.commit();
    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 after commit", (odmgZoosBefore + 10), odmgZoosAfter);
    assertEquals(
        "Wrong number of projects found after commit", (projectsBefore + 5), projectsAfter);
    assertEquals(
        "Wrong number of odmgZoos found after commit", (odmgZoosBefore + 10), odmgZoosAfterOQL);
    assertEquals(
        "Wrong number of projects found after commit", (projectsBefore + 5), projectsAfterOQL);

    /*
    Here we test if we can see our changes while the transaction runs. IMO it must be
    possible to see all changes made in a transaction.
    */

    assertEquals(
        "Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 5), odmgZoosWhilePB);
    assertEquals(
        "Wrong number of projects found while transaction", (projectsBefore + 3), projectsWhilePB);
    assertEquals(
        "Wrong number of odmgZoos found while transaction",
        (odmgZoosBefore + 10),
        odmgZoosWhilePB2);
    assertEquals(
        "Wrong number of projects found while transaction", (projectsBefore + 5), projectsWhilePB2);
    assertEquals(
        "Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 5), odmgZoosWhile);
    assertEquals(
        "Wrong number of projects found while transaction", (projectsBefore + 3), projectsWhile);
    assertEquals(
        "Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 10), odmgZoosWhile2);
    assertEquals(
        "Wrong number of projects found while transaction", (projectsBefore + 5), projectsWhile2);

    // after tx another tx should be able to lock these objects
    tx2 = (TransactionImpl) odmg.newTransaction();
    tx2.begin();
    try {
      Iterator it = zoos.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }

      it = projects.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }
    } finally {
      tx2.abort();
    }
  }