Search in sources :

Example 46 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class TestTransactionStore method testSavepoint.

private void testSavepoint() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx;
    TransactionMap<String, String> m;
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hello");
    m.put("2", "World");
    m.put("1", "Hallo");
    m.remove("2");
    m.put("3", "!");
    long logId = tx.setSavepoint();
    m.put("1", "Hi");
    m.put("2", ".");
    m.remove("3");
    tx.rollbackToSavepoint(logId);
    assertEquals("Hallo", m.get("1"));
    assertNull(m.get("2"));
    assertEquals("!", m.get("3"));
    tx.rollback();
    tx = ts.begin();
    m = tx.openMap("test");
    assertNull(m.get("1"));
    assertNull(m.get("2"));
    assertNull(m.get("3"));
    ts.close();
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction)

Example 47 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class TestTransactionStore method testStoreMultiThreadedReads.

private static void testStoreMultiThreadedReads() throws Exception {
    MVStore s = MVStore.open(null);
    final TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction t = ts.begin();
    TransactionMap<Integer, Integer> mapA = t.openMap("a");
    mapA.put(1, 0);
    t.commit();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            for (int i = 0; !stop; i++) {
                Transaction tx = ts.begin();
                TransactionMap<Integer, Integer> mapA = tx.openMap("a");
                while (!mapA.tryPut(1, i)) {
                // repeat
                }
                tx.commit();
                // map B transaction
                // the other thread will get a map A uncommitted value,
                // but by the time it tries to walk back to the committed
                // value, the undoLog has changed
                tx = ts.begin();
                TransactionMap<Integer, Integer> mapB = tx.openMap("b");
                // put a new value to the map; this will cause a map B
                // undoLog entry to be created with a null pre-image value
                mapB.tryPut(i, -i);
                // this is where the real race condition occurs:
                // some other thread might get the B log entry
                // for this transaction rather than the uncommitted A log
                // entry it is expecting
                tx.commit();
            }
        }
    };
    task.execute();
    try {
        for (int i = 0; i < 10000; i++) {
            Transaction tx = ts.begin();
            mapA = tx.openMap("a");
            if (mapA.get(1) == null) {
                throw new AssertionError("key not found");
            }
            tx.commit();
        }
    } finally {
        task.get();
    }
    ts.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Task(org.h2.util.Task) Transaction(org.h2.mvstore.db.TransactionStore.Transaction)

Example 48 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project h2database by h2database.

the class TestTransactionStore method testGetModifiedMaps.

private void testGetModifiedMaps() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx;
    TransactionMap<String, String> m1, m2, m3;
    long sp;
    tx = ts.begin();
    m1 = tx.openMap("m1");
    m2 = tx.openMap("m2");
    m3 = tx.openMap("m3");
    assertFalse(tx.getChanges(0).hasNext());
    tx.commit();
    tx = ts.begin();
    m1 = tx.openMap("m1");
    m2 = tx.openMap("m2");
    m3 = tx.openMap("m3");
    m1.put("1", "100");
    sp = tx.setSavepoint();
    m2.put("1", "100");
    m3.put("1", "100");
    Iterator<Change> it = tx.getChanges(sp);
    assertTrue(it.hasNext());
    Change c;
    c = it.next();
    assertEquals("m3", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m2", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());
    it = tx.getChanges(0);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m3", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m2", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m1", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());
    tx.rollbackToSavepoint(sp);
    it = tx.getChanges(0);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m1", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());
    tx.commit();
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) Change(org.h2.mvstore.db.TransactionStore.Change)

Example 49 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project narayana by jbosstm.

the class RecoveryTest method test.

@Test
@BMRule(name = "throw lang error exception", targetClass = "org.h2.jdbcx.JdbcXAConnection", targetMethod = "commit", action = "throw new java.lang.Error()", targetLocation = "AT ENTRY")
public void test() throws Exception {
    String url = "jdbc:arjuna:";
    Properties p = System.getProperties();
    p.put("jdbc.drivers", Driver.class.getName());
    System.setProperties(p);
    transactionalDriver = new TransactionalDriver();
    DriverManager.registerDriver(transactionalDriver);
    Properties dbProperties = new Properties();
    final JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    dbProperties.put(TransactionalDriver.XADataSource, ds);
    step0_setupTables(url, dbProperties);
    // We need to do this in a different thread as otherwise the transaction would still be associated with the connection due to the java.lang.Error
    // RMFAIL on it's own will cause H2 to close connection and that seems to discard the indoubt transactions
    step1_leaveXidsInDbTable(url, dbProperties);
    step2_checkDbIsNotCommitted(url, dbProperties);
    // 3. Perform recovery
    {
        XARecoveryModule xarm = new XARecoveryModule();
        xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

            @Override
            public boolean initialise(String p) throws Exception {
                return false;
            }

            @Override
            public XAResource[] getXAResources() throws Exception {
                return new XAResource[] { ds.getXAConnection().getXAResource() };
            }
        });
        RecoveryManager manager = RecoveryManager.manager();
        manager.removeAllModules(true);
        manager.addModule(xarm);
        AtomicActionRecoveryModule aarm = new AtomicActionRecoveryModule();
        aarm.periodicWorkFirstPass();
        Transformer.disableTriggers(true);
        aarm.periodicWorkSecondPass();
        Transformer.enableTriggers(true);
    }
    step4_finalDbCommitCheck(url, dbProperties);
}
Also used : RecoveryManager(com.arjuna.ats.arjuna.recovery.RecoveryManager) XAResource(javax.transaction.xa.XAResource) AtomicActionRecoveryModule(com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) Driver(org.h2.Driver) Properties(java.util.Properties) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) BMRule(org.jboss.byteman.contrib.bmunit.BMRule) Test(org.junit.Test)

Example 50 with Transaction

use of org.h2.mvstore.db.TransactionStore.Transaction in project narayana by jbosstm.

the class TestCommitMarkableResourceFailAfterPrepareTwoXAResources method testFailAfterPrepare.

@Test
@BMScript("commitMarkableResourceFailAfterPrepare")
public void testFailAfterPrepare() throws Exception {
    final DataSource dataSource = new JdbcDataSource();
    ((JdbcDataSource) dataSource).setURL("jdbc:h2:mem:JBTMDB;MVCC=TRUE;DB_CLOSE_DELAY=-1");
    // Test code
    Utils.createTables(dataSource.getConnection());
    // We can't just instantiate one as we need to be using the
    // same one as
    // the transaction
    // manager would have used to mark the transaction for GC
    CommitMarkableResourceRecordRecoveryModule recoveryModule = null;
    XARecoveryModule xarm = null;
    Vector recoveryModules = manager.getModules();
    if (recoveryModules != null) {
        Enumeration modules = recoveryModules.elements();
        while (modules.hasMoreElements()) {
            RecoveryModule m = (RecoveryModule) modules.nextElement();
            if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
                recoveryModule = (CommitMarkableResourceRecordRecoveryModule) m;
            } else if (m instanceof XARecoveryModule) {
                xarm = (XARecoveryModule) m;
                xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {

                    public boolean initialise(String p) throws Exception {
                        return true;
                    }

                    public XAResource[] getXAResources() throws Exception {
                        return new XAResource[] { xaResource, xaResource2 };
                    }
                });
            }
        }
    }
    // final Object o = new Object();
    // synchronized (o) {
    Thread foo = new Thread(new Runnable() {

        public void run() {
            try {
                javax.transaction.TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
                tm.begin();
                Connection localJDBCConnection = dataSource.getConnection();
                localJDBCConnection.setAutoCommit(false);
                nonXAResource = new JDBCConnectableResource(localJDBCConnection);
                tm.getTransaction().enlistResource(nonXAResource);
                xaResource = new SimpleXAResource();
                tm.getTransaction().enlistResource(xaResource);
                xaResource2 = new SimpleXAResource2();
                tm.getTransaction().enlistResource(xaResource2);
                localJDBCConnection.createStatement().execute("INSERT INTO foo (bar) VALUES (1)");
                tm.commit();
            } catch (ExecuteException t) {
            } catch (Exception t) {
                t.printStackTrace();
                failed = true;
            } catch (Error t) {
            }
        }
    });
    foo.start();
    foo.join();
    assertFalse(failed);
    // This is test code, it allows us to verify that the
    // correct XID was
    // removed
    Xid committed = ((JDBCConnectableResource) nonXAResource).getStartedXid();
    assertNotNull(committed);
    // The recovery module has to perform lookups
    new InitialContext().rebind("commitmarkableresource", dataSource);
    // Run the first pass it will load the committed Xids into memory
    manager.scan();
    assertFalse(recoveryModule.wasCommitted("commitmarkableresource", committed));
    // Now we need to correctly complete the transaction
    assertFalse(xaResource.wasCommitted());
    assertFalse(xaResource.wasRolledback());
    SimpleXAResource2.injectRollbackError();
    boolean scanned = false;
    try {
        manager.scan();
        scanned = true;
    } catch (Error error) {
        // This is expected from xaResource2, it is intended to simulate a
        // crash
        // Should clear off the scanning flag only
        xarm.periodicWorkSecondPass();
    }
    if (scanned) {
        fail("Should have failed scan");
    }
    assertFalse(xaResource.wasCommitted());
    assertTrue(xaResource.wasRolledback());
    assertFalse(xaResource2.commitCalled());
    assertFalse(xaResource2.rollbackCalled());
    RecoveryManagerImple recoveryManagerImple = new RecoveryManagerImple(false);
    recoveryManagerImple.scan();
    assertFalse(xaResource2.commitCalled());
    assertTrue(xaResource2.rollbackCalled());
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) RecoveryManagerImple(com.arjuna.ats.internal.arjuna.recovery.RecoveryManagerImple) XAResourceRecoveryHelper(com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) Vector(java.util.Vector) Enumeration(java.util.Enumeration) Connection(java.sql.Connection) ExecuteException(org.jboss.byteman.rule.exception.ExecuteException) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) CommitMarkableResourceRecordRecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule) RecoveryModule(com.arjuna.ats.arjuna.recovery.RecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) Test(org.junit.Test) BMScript(org.jboss.byteman.contrib.bmunit.BMScript)

Aggregations

Transaction (org.h2.mvstore.db.TransactionStore.Transaction)22 MVStore (org.h2.mvstore.MVStore)18 TransactionStore (org.h2.mvstore.db.TransactionStore)18 Connection (java.sql.Connection)13 ResultSet (java.sql.ResultSet)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)8 DbException (org.h2.message.DbException)8 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)7 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)7 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)7 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)7 PreparedStatement (java.sql.PreparedStatement)7 Statement (java.sql.Statement)7 Enumeration (java.util.Enumeration)7 XAResource (javax.transaction.xa.XAResource)7 Test (org.junit.Test)7 SQLException (java.sql.SQLException)6 Vector (java.util.Vector)6 Constraint (org.h2.constraint.Constraint)6