Search in sources :

Example 36 with Transaction

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

the class TestMVTableEngine method testTransactionLogUsuallyNotStored.

private void testTransactionLogUsuallyNotStored() throws Exception {
    Connection conn;
    Statement stat;
    // we expect the transaction log is empty in at least some of the cases
    for (int test = 0; test < 5; test++) {
        deleteDb(getTestName());
        String url = getTestName() + ";MV_STORE=TRUE";
        url = getURL(url, true);
        conn = getConnection(url);
        stat = conn.createStatement();
        stat.execute("create table test(id identity, name varchar)");
        conn.setAutoCommit(false);
        PreparedStatement prep = conn.prepareStatement("insert into test(name) values(space(10000))");
        for (int j = 0; j < 100; j++) {
            for (int i = 0; i < 100; i++) {
                prep.execute();
            }
            conn.commit();
        }
        stat.execute("shutdown immediately");
        JdbcUtils.closeSilently(conn);
        String file = getBaseDir() + "/" + getTestName() + Constants.SUFFIX_MV_FILE;
        MVStore store = MVStore.open(file);
        TransactionStore t = new TransactionStore(store);
        t.init();
        int openTransactions = t.getOpenTransactions().size();
        store.close();
        if (openTransactions == 0) {
            return;
        }
    }
    fail("transaction log was never empty");
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) Savepoint(java.sql.Savepoint)

Example 37 with Transaction

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

the class TestMVTableEngine method testCount.

private void testCount() throws Exception {
    if (config.memory) {
        return;
    }
    Connection conn;
    Connection conn2;
    Statement stat;
    Statement stat2;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE;MVCC=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int)");
    stat.execute("create table test2(id int)");
    stat.execute("insert into test select x from system_range(1, 10000)");
    conn.close();
    ResultSet rs;
    String plan;
    conn2 = getConnection(url);
    stat2 = conn2.createStatement();
    rs = stat2.executeQuery("explain analyze select count(*) from test");
    rs.next();
    plan = rs.getString(1);
    assertTrue(plan, plan.indexOf("reads:") < 0);
    conn = getConnection(url);
    stat = conn.createStatement();
    conn.setAutoCommit(false);
    stat.execute("insert into test select x from system_range(1, 1000)");
    rs = stat.executeQuery("select count(*) from test");
    rs.next();
    assertEquals(11000, rs.getInt(1));
    // not yet committed
    rs = stat2.executeQuery("explain analyze select count(*) from test");
    rs.next();
    plan = rs.getString(1);
    // transaction log is small, so no need to read the table
    assertTrue(plan, plan.indexOf("reads:") < 0);
    rs = stat2.executeQuery("select count(*) from test");
    rs.next();
    assertEquals(10000, rs.getInt(1));
    stat.execute("insert into test2 select x from system_range(1, 11000)");
    rs = stat2.executeQuery("explain analyze select count(*) from test");
    rs.next();
    plan = rs.getString(1);
    // transaction log is larger than the table, so read the table
    assertContains(plan, "reads:");
    rs = stat2.executeQuery("select count(*) from test");
    rs.next();
    assertEquals(10000, rs.getInt(1));
    conn2.close();
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 38 with Transaction

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

the class TestTransactionStore method testConcurrentAdd.

private void testConcurrentAdd() {
    MVStore s;
    s = MVStore.open(null);
    final TransactionStore ts = new TransactionStore(s);
    ts.init();
    final Random r = new Random(1);
    final AtomicInteger key = new AtomicInteger();
    final AtomicInteger failCount = new AtomicInteger();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            Transaction tx = null;
            TransactionMap<Integer, Integer> map = null;
            while (!stop) {
                int k = key.get();
                tx = ts.begin();
                map = tx.openMap("data");
                try {
                    map.put(k, r.nextInt());
                } catch (IllegalStateException e) {
                    failCount.incrementAndGet();
                // ignore and retry
                }
                tx.commit();
            }
        }
    };
    task.execute();
    Transaction tx = null;
    int count = 100000;
    TransactionMap<Integer, Integer> map = null;
    for (int i = 0; i < count; i++) {
        int k = i;
        key.set(k);
        tx = ts.begin();
        map = tx.openMap("data");
        try {
            map.put(k, r.nextInt());
        } catch (IllegalStateException e) {
            failCount.incrementAndGet();
        // ignore and retry
        }
        tx.commit();
        if (failCount.get() > 0 && i > 4000) {
            // stop earlier, if possible
            count = i;
            break;
        }
    }
    // we expect at least 10% the operations were successful
    assertTrue(failCount.toString() + " >= " + (count * 0.9), failCount.get() < count * 0.9);
    // we expect at least a few failures
    assertTrue(failCount.toString(), failCount.get() > 0);
    s.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) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 39 with Transaction

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

the class TestTransactionStore method testSingleConnection.

private void testSingleConnection() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx;
    TransactionMap<String, String> m;
    // add, rollback
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hello");
    assertEquals("Hello", m.get("1"));
    m.put("2", "World");
    assertEquals("World", m.get("2"));
    tx.rollback();
    tx = ts.begin();
    m = tx.openMap("test");
    assertNull(m.get("1"));
    assertNull(m.get("2"));
    // add, commit
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hello");
    m.put("2", "World");
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    tx.commit();
    tx = ts.begin();
    m = tx.openMap("test");
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    // update+delete+insert, rollback
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hallo");
    m.remove("2");
    m.put("3", "!");
    assertEquals("Hallo", m.get("1"));
    assertNull(m.get("2"));
    assertEquals("!", m.get("3"));
    tx.rollback();
    tx = ts.begin();
    m = tx.openMap("test");
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    assertNull(m.get("3"));
    // update+delete+insert, commit
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hallo");
    m.remove("2");
    m.put("3", "!");
    assertEquals("Hallo", m.get("1"));
    assertNull(m.get("2"));
    assertEquals("!", m.get("3"));
    tx.commit();
    tx = ts.begin();
    m = tx.openMap("test");
    assertEquals("Hallo", m.get("1"));
    assertNull(m.get("2"));
    assertEquals("!", 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 40 with Transaction

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

the class TestTransactionStore method testMultiStatement.

/**
 * Tests behavior when used for a sequence of SQL statements. Each statement
 * uses a savepoint. Within a statement, changes by the statement itself are
 * not seen; the change is only seen when the statement finished.
 * <p>
 * Update statements that change the key of multiple rows may use delete/add
 * pairs to do so (they don't need to first delete all entries and then
 * re-add them). Trying to add multiple values for the same key is not
 * allowed (an update statement that would result in a duplicate key).
 */
private void testMultiStatement() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx;
    TransactionMap<String, String> m;
    long startUpdate;
    tx = ts.begin();
    // start of statement
    // create table test
    startUpdate = tx.setSavepoint();
    m = tx.openMap("test");
    m.setSavepoint(startUpdate);
    // start of statement
    // insert into test(id, name) values(1, 'Hello'), (2, 'World')
    startUpdate = tx.setSavepoint();
    m.setSavepoint(startUpdate);
    assertTrue(m.trySet("1", "Hello", true));
    assertTrue(m.trySet("2", "World", true));
    // not seen yet (within the same statement)
    assertNull(m.get("1"));
    assertNull(m.get("2"));
    // start of statement
    startUpdate = tx.setSavepoint();
    // now we see the newest version
    m.setSavepoint(startUpdate);
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    // update test set primaryKey = primaryKey + 1
    // (this is usually a tricky case)
    assertEquals("Hello", m.get("1"));
    assertTrue(m.trySet("1", null, true));
    assertTrue(m.trySet("2", "Hello", true));
    assertEquals("World", m.get("2"));
    // already updated by this statement, so it has no effect
    // but still returns true because it was changed by this transaction
    assertTrue(m.trySet("2", null, true));
    assertTrue(m.trySet("3", "World", true));
    // not seen within this statement
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    assertNull(m.get("3"));
    // start of statement
    startUpdate = tx.setSavepoint();
    m.setSavepoint(startUpdate);
    // select * from test
    assertNull(m.get("1"));
    assertEquals("Hello", m.get("2"));
    assertEquals("World", m.get("3"));
    // start of statement
    startUpdate = tx.setSavepoint();
    m.setSavepoint(startUpdate);
    // update test set id = 1
    // should fail: duplicate key
    assertTrue(m.trySet("2", null, true));
    assertTrue(m.trySet("1", "Hello", true));
    assertTrue(m.trySet("3", null, true));
    assertFalse(m.trySet("1", "World", true));
    tx.rollbackToSavepoint(startUpdate);
    startUpdate = tx.setSavepoint();
    m.setSavepoint(startUpdate);
    assertNull(m.get("1"));
    assertEquals("Hello", m.get("2"));
    assertEquals("World", m.get("3"));
    tx.commit();
    ts.close();
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction)

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