Package org.helidb.txn

Examples of org.helidb.txn.Transaction


  public void testDeleteRollback()
  {
    Database<Integer, Long> db = createDatabaseWoTxn();
    try
    {
      Transaction txn = Transaction.startTransaction(false);
      try
      {
        db.insert(1, 10L);
        db.insert(2, 20L);
        db.insert(3, 30L);
        txn.commit();

        txn = Transaction.startTransaction(false);
        db.delete(2);
        txn.rollback();

        txn = Transaction.startTransaction(true);
        assertEquals(3, db.size());
        assertEquals(30L, db.get(3).longValue());
        assertEquals(10L, db.get(1).longValue());
        assertEquals(20L, db.get(2).longValue());
      }
      finally
      {
        txn.rollback();
      }
    }
    finally
    {
      tearDownDatabase(db);
View Full Code Here


  public void testCommitRightValue()
  {
    Database<Integer, Long> db = createDatabaseWoTxn();
    try
    {
      Transaction txn = Transaction.startTransaction(false);
      try
      {
        db.insert(1, 10L);
        db.insert(2, 20L);
        txn.commit();

        txn = Transaction.startTransaction(false);
        db.update(1, 1000L);
        db.update(1, 10000L);
        txn.commit();

        txn = Transaction.startTransaction(true);
        assertEquals(2, db.size());
        assertEquals(20L, db.get(2).longValue());
        assertEquals(10000L, db.get(1).longValue());
      }
      finally
      {
        txn.rollback();
      }
    }
    finally
    {
      tearDownDatabase(db);
View Full Code Here

  public void testRollbackToRightValue()
  {
    Database<Integer, Long> db = createDatabaseWoTxn();
    try
    {
      Transaction txn = Transaction.startTransaction(false);
      try
      {
        db.insert(1, 10L);
        db.insert(2, 20L);
        txn.commit();

        txn = Transaction.startTransaction(false);
        db.update(1, 100L);
        db.update(2, 200L);
        txn.commit();

        txn = Transaction.startTransaction(false);
        db.update(1, 1000L);
        db.update(1, 10000L);
        txn.rollback();

        txn = Transaction.startTransaction(true);
        assertEquals(2, db.size());
        assertEquals(200L, db.get(2).longValue());
        assertEquals(100L, db.get(1).longValue());
      }
      finally
      {
        txn.rollback();
      }
    }
    finally
    {
      tearDownDatabase(db);
View Full Code Here

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

      Transaction txn = Transaction.startTransaction(false);
      try
      {
        db.fasterInsert(Integer.valueOf(12), Long.valueOf(345823L));
        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);
              // Now the database is closed.
              assertFalse(((AtomicBoolean) mp.get("flag1")).get());
            }
            finally
            {
              txn.rollback();
            }
          }
          finally
          {
            if (l1 != null)
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<Integer, Long> db2 = createDatabaseWoTxn();
      try
      {
        populateDb(db2);

        Transaction txn = Transaction.startTransaction(false);
        try
        {
          db1.update(Integer.valueOf(1), Long.valueOf(1235L));
          db2.update(Integer.valueOf(1), Long.valueOf(2432398L));
          db2.fasterInsert(Integer.valueOf(438), Long.valueOf(248238L));

          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(Long.valueOf(2), db1.get(Integer.valueOf(1)));
          assertEquals(Long.valueOf(2), db2.get(Integer.valueOf(1)));
          assertNull(db2.get(Integer.valueOf(438)));
        }
        finally
        {
          txn.rollback();
        }
      }
      finally
      {
        tearDownDatabase(db2);
View Full Code Here

  {
    TransactionalDatabase<Integer, Long> db1 = createDatabaseWoTxn();
    try
    {
      populateDb(db1);
      Transaction txn = Transaction.startTransaction(false);
      try
      {
        db1.delete(1);
        db1.update(123, 10000L);
        db1.insert(2, 2L);

        TransactionalDatabase<Integer, Long> db2 = createNewDatabaseUsingSameFiles(db1);
        try
        {
          // This should contain the old values.
          assertEquals(234L, db2.get(123).longValue());
          assertEquals(2L, db2.get(1).longValue());
        }
        finally
        {
          tearDownDatabase(db2);
        }
      }
      finally
      {
        if (!txn.isFinished())
        {
          txn.rollback();
        }
      }
    }
    finally
    {
View Full Code Here

public class TransactionTest
{
  @Test
  public void testClearTransactionAtCommit()
  {
    Transaction txn = Transaction.startTransaction(false);
    assertSame(txn, Transaction.getCurrentTransaction());
    txn.commit();
    assertNull(Transaction.getCurrentTransactionOrNull());
  }
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.