Package org.helidb.txn

Examples of org.helidb.txn.Transaction


  public void testRollbackToRightValue()
  {
    Database<String, String> db = createDatabaseWoTxn();
    try
    {
      Transaction txn = Transaction.startTransaction(false);
      try
      {
        db.insert("key 1", "value 1");
        db.insert("key 2", "value 2");
        txn.commit();

        txn = Transaction.startTransaction(false);
        db.update("key 1", "value 1p");
        db.update("key 2", "value 2p");
        txn.commit();

        txn = Transaction.startTransaction(false);
        db.update("key 1", "value 1pp");
        db.update("key 1", "value 1ppp");
        txn.rollback();

        txn = Transaction.startTransaction(true);
        assertEquals(2, db.size());
        assertEquals("value 2p", db.get("key 2"));
        assertEquals("value 1p", db.get("key 1"));
      }
      finally
      {
        txn.rollback();
      }
    }
    finally
    {
      tearDownDatabase(db);
View Full Code Here


    TransactionalDatabase<String, String> db = createDatabaseWoTxn();
    try
    {
      populateDb(db);

      Transaction txn = Transaction.startTransaction(false);
      try
      {
        db.fasterInsert("key11", "value11");
        db.close();
      }
      finally
      {
        txn.rollback();
      }
    }
    finally
    {
      tearDownDatabase(db);
View Full Code Here

        {
          CountDownLatch l1 = (CountDownLatch) mp.get("latch1");
          try
          {
            TransactionalDatabase<?, ?> tdb = (TransactionalDatabase<?, ?>) mp.get("db");
            Transaction txn = Transaction.startTransaction(true);
            try
            {
              tdb.joinTransaction(true);

              l1.countDown();
              l1.await();
              l1 = null;

              Thread.sleep(1000);
              assertFalse(((AtomicBoolean) mp.get("flag1")).get());
            }
            finally
            {
              txn.rollback();
            }
            // Now the database is closed.
          }
          finally
          {
View Full Code Here

          CountDownLatch l1 = (CountDownLatch) mp.get("latch1");
          try
          {
            TransactionalDatabase<?, ?> tdb = (TransactionalDatabase<?, ?>) mp.get("db");
            // Start a transaction and count down the latch
            Transaction txn = Transaction.startTransaction(false);
            try
            {
              // Let the database join the transaction
              tdb.joinTransaction(false);
              l1.countDown();
              l1.await();
              l1 = null;

              // Sleep awhile to give the other thread plenty of
              // time to set the flag
              Thread.sleep(1000);
              // Still not set
              assertFalse(((AtomicBoolean) mp.get("flag1")).get());
            }
            finally
            {
              txn.rollback();
            }
          }
          finally
          {
            if (l1 != null)
            {
              l1.countDown();
            }
          }
        }
      } }, new TestStep[] { new TestStep() {
        public void runStep(Map<String, Object> mp) throws Exception
        {
          CountDownLatch l1 = (CountDownLatch) mp.get("latch1");
          try
          {
            TransactionalDatabase<?, ?> tdb = (TransactionalDatabase<?, ?>) mp.get("db");
            Transaction txn = Transaction.startTransaction(false);
            try
            {
              l1.countDown();
              l1.await();
              l1 = null;
              // Try to join the transaction. This should not
              // be possible until the other thread leaves it
              tdb.joinTransaction(false);
              ((AtomicBoolean) mp.get("flag1")).set(true);
            }
            finally
            {
              txn.rollback();
            }
          }
          finally
          {
            if (l1 != null)
View Full Code Here

            try
            {
              TransactionalDatabase<?, ?> tdb1 = (TransactionalDatabase<?, ?>) mp.get("db1");
              TransactionalDatabase<?, ?> tdb2 = (TransactionalDatabase<?, ?>) mp.get("db2");
              // Start a transaction and count down the latch
              Transaction txn = Transaction.startTransaction(false);
              try
              {
                // Let the databases join the transaction
                tdb1.joinTransaction(false);
                tdb2.joinTransaction(false);
                l1.countDown();
                l1.await();
                l1 = null;

                // Sleep awhile to give the other thread plenty
                // of time to set the flag
                Thread.sleep(1000);
                // Still not set
                assertFalse(((AtomicBoolean) mp.get("flag1")).get());
              }
              finally
              {
                txn.rollback();
              }
            }
            finally
            {
              if (l1 != null)
              {
                l1.countDown();
              }
            }
          }
        } }, new TestStep[] { new TestStep() {
          public void runStep(Map<String, Object> mp) throws Exception
          {
            CountDownLatch l1 = (CountDownLatch) mp.get("latch1");
            try
            {
              TransactionalDatabase<?, ?> tdb1 = (TransactionalDatabase<?, ?>) mp.get("db1");
              TransactionalDatabase<?, ?> tdb2 = (TransactionalDatabase<?, ?>) mp.get("db2");
              Transaction txn = Transaction.startTransaction(false);
              try
              {
                l1.countDown();
                l1.await();
                l1 = null;
                // Try to join the transaction. This should not
                // be possible until the other thread leaves it
                tdb1.joinTransaction(false);
                tdb2.joinTransaction(false);
                ((AtomicBoolean) mp.get("flag1")).set(true);
              }
              finally
              {
                txn.rollback();
              }
            }
            finally
            {
              if (l1 != null)
View Full Code Here

      TransactionalDatabase<String, String> db2 = createDatabaseWoTxn();
      try
      {
        populateDb(db2);

        Transaction txn = Transaction.startTransaction(false);
        try
        {
          db1.update("key1", "value1p");
          db2.update("key1", "value1p");
          db2.fasterInsert("key11", "value11");

          injectCannotCommit(txn.getCollaborator(db2));

          try
          {
            txn.commit();
            fail();
          }
          catch (UnableToCommitException e)
          {
            assertTrue(e.getMessage().contains("inject"));
          }
          assertTrue(txn.isFinished());
          txn = null;
        }
        finally
        {
          if (txn != null)
          {
            txn.rollback();
          }
        }

        // Verify that nothing was updated in the databases
        txn = Transaction.startTransaction(true);
        try
        {
          assertEquals("value1", db1.get("key1"));
          assertEquals("value1", db2.get("key1"));
          assertNull(db2.get("key11"));
        }
        finally
        {
          txn.rollback();
        }
      }
      finally
      {
        tearDownDatabase(db2);
View Full Code Here

  {
    TransactionalDatabase<String, String> db1 = createDatabaseWoTxn();
    try
    {
      populateDb(db1);
      Transaction txn = Transaction.startTransaction(false);
      try
      {
        db1.delete("key1");
        db1.update("key10", "value10p");
        db1.insert("key2", "value2");

        TransactionalDatabase<String, String> db2 = createNewDatabaseUsingSameFiles(db1);
        try
        {
          // This should contain the old values.
          assertEquals("value1", db2.get("key1"));
          assertEquals("value10", db2.get("key10"));
        }
        finally
        {
          tearDownDatabase(db2);
        }
      }
      finally
      {
        if (!txn.isFinished())
        {
          txn.rollback();
        }
      }
    }
    finally
    {
View Full Code Here

  }

  @Override
  protected void tearDownDatabase(Database<?, ?> db)
  {
    Transaction txn = Transaction.getCurrentTransactionOrNull();
    // The transaction may already be rolled back if we're working with
    // several databases.
    if (txn != null)
    {
      if (!txn.isFinished())
      {
        txn.rollback();
      }
    }

    db.close();
    Directory d = m_dbDirs.remove(System.identityHashCode(db));
View Full Code Here

   * Load some data into the database.
   */
  protected void populateDb(Database<Integer, Long> db)
  {
    boolean successful = false;
    Transaction txn = Transaction.startTransaction(false);
    try
    {
      db.fasterInsert(Integer.valueOf(1), Long.valueOf(2));
      db.fasterInsert(Integer.valueOf(123), Long.valueOf(234));
      assertEquals(Long.valueOf(2), db.get(Integer.valueOf(1)));
      assertEquals(Long.valueOf(234), db.get(Integer.valueOf(123)));
      txn.commit();
      successful = true;
    }
    catch (RuntimeException e)
    {
      e.printStackTrace();
    }
    finally
    {
      if (!successful && !txn.isFinished())
      {
        txn.rollback();
      }
    }
    assertNull(Transaction.getCurrentTransactionOrNull());
  }
View Full Code Here

    Database<Integer, Long> db = createDatabaseWoTxn();
    try
    {
      populateDb(db);

      Transaction txn = Transaction.startTransaction(true);
      try
      {
        assertEquals(Long.valueOf(2), db.get(Integer.valueOf(1)));
        assertEquals(Long.valueOf(234), db.get(Integer.valueOf(123)));
      }
      finally
      {
        txn.rollback();
      }
    }
    finally
    {
      tearDownDatabase(db);
View Full Code Here

TOP

Related Classes of org.helidb.txn.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.