Search in sources :

Example 6 with Transaction

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

the class Session method rollbackTo.

/**
 * Partially roll back the current transaction.
 *
 * @param savepoint the savepoint to which should be rolled back
 * @param trimToSize if the list should be trimmed
 */
public void rollbackTo(Savepoint savepoint, boolean trimToSize) {
    int index = savepoint == null ? 0 : savepoint.logIndex;
    while (undoLog.size() > index) {
        UndoLogRecord entry = undoLog.getLast();
        entry.undo(this);
        undoLog.removeLast(trimToSize);
    }
    if (transaction != null) {
        long savepointId = savepoint == null ? 0 : savepoint.transactionSavepoint;
        HashMap<String, MVTable> tableMap = database.getMvStore().getTables();
        Iterator<Change> it = transaction.getChanges(savepointId);
        while (it.hasNext()) {
            Change c = it.next();
            MVTable t = tableMap.get(c.mapName);
            if (t != null) {
                long key = ((ValueLong) c.key).getLong();
                ValueArray value = (ValueArray) c.value;
                short op;
                Row row;
                if (value == null) {
                    op = UndoLogRecord.INSERT;
                    row = t.getRow(this, key);
                } else {
                    op = UndoLogRecord.DELETE;
                    row = createRow(value.getList(), Row.MEMORY_CALCULATE);
                }
                row.setKey(key);
                UndoLogRecord log = new UndoLogRecord(t, op, row);
                log.undo(this);
            }
        }
    }
    if (savepoints != null) {
        String[] names = savepoints.keySet().toArray(new String[savepoints.size()]);
        for (String name : names) {
            Savepoint sp = savepoints.get(name);
            int savepointIndex = sp.logIndex;
            if (savepointIndex > index) {
                savepoints.remove(name);
            }
        }
    }
}
Also used : ValueLong(org.h2.value.ValueLong) MVTable(org.h2.mvstore.db.MVTable) ValueString(org.h2.value.ValueString) Change(org.h2.mvstore.db.TransactionStore.Change) Constraint(org.h2.constraint.Constraint) Row(org.h2.result.Row) ValueArray(org.h2.value.ValueArray)

Example 7 with Transaction

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

the class MVSecondaryIndex method remove.

@Override
public void remove(Session session) {
    TransactionMap<Value, Value> map = getMap(session);
    if (!map.isClosed()) {
        Transaction t = session.getTransaction();
        t.removeMap(map);
    }
}
Also used : Transaction(org.h2.mvstore.db.TransactionStore.Transaction) Value(org.h2.value.Value)

Example 8 with Transaction

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

the class TestMVTableEngine method testTwoPhaseCommit.

private void testTwoPhaseCommit() throws Exception {
    if (config.memory) {
        return;
    }
    Connection conn;
    Statement stat;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar)");
    stat.execute("set write_delay 0");
    conn.setAutoCommit(false);
    stat.execute("insert into test values(1, 'Hello')");
    stat.execute("prepare commit test_tx");
    stat.execute("shutdown immediately");
    JdbcUtils.closeSilently(conn);
    conn = getConnection(url);
    stat = conn.createStatement();
    ResultSet rs;
    rs = stat.executeQuery("select * from information_schema.in_doubt");
    assertTrue(rs.next());
    stat.execute("commit transaction test_tx");
    rs = stat.executeQuery("select * from test");
    assertTrue(rs.next());
    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 9 with Transaction

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

the class TestTransactionStore method testTransactionAge.

private void testTransactionAge() throws Exception {
    MVStore s;
    TransactionStore ts;
    s = MVStore.open(null);
    ts = new TransactionStore(s);
    ts.init();
    ts.setMaxTransactionId(16);
    ArrayList<Transaction> openList = new ArrayList<>();
    for (int i = 0, j = 1; i < 64; i++) {
        Transaction t = ts.begin();
        openList.add(t);
        assertEquals(j, t.getId());
        j++;
        if (j > 16) {
            j = 1;
        }
        if (openList.size() >= 16) {
            t = openList.remove(0);
            t.commit();
        }
    }
    s = MVStore.open(null);
    ts = new TransactionStore(s);
    ts.init();
    ts.setMaxTransactionId(16);
    ArrayList<Transaction> fifo = New.arrayList();
    int open = 0;
    for (int i = 0; i < 64; i++) {
        Transaction t = null;
        if (open >= 16) {
            try {
                t = ts.begin();
                fail();
            } catch (IllegalStateException e) {
            // expected - too many open
            }
            Transaction first = fifo.remove(0);
            first.commit();
            open--;
        }
        t = ts.begin();
        t.openMap("data").put(i, i);
        fifo.add(t);
        open++;
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) ArrayList(java.util.ArrayList)

Example 10 with Transaction

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

the class TestTransactionStore method testConcurrentTransactionsReadCommitted.

private void testConcurrentTransactionsReadCommitted() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx1, tx2;
    TransactionMap<String, String> m1, m2;
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    m1.put("1", "Hi");
    m1.put("3", ".");
    tx1.commit();
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    m1.put("1", "Hello");
    m1.put("2", "World");
    m1.remove("3");
    tx1.commit();
    // start new transaction to read old data
    tx2 = ts.begin();
    m2 = tx2.openMap("test");
    // start transaction tx1, update/delete/add
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    m1.put("1", "Hallo");
    m1.remove("2");
    m1.put("3", "!");
    assertEquals("Hello", m2.get("1"));
    assertEquals("World", m2.get("2"));
    assertNull(m2.get("3"));
    tx1.commit();
    assertEquals("Hallo", m2.get("1"));
    assertNull(m2.get("2"));
    assertEquals("!", m2.get("3"));
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    m1.put("2", "World");
    assertNull(m2.get("2"));
    assertFalse(m2.tryRemove("2"));
    assertFalse(m2.tryPut("2", "Welt"));
    tx2 = ts.begin();
    m2 = tx2.openMap("test");
    assertNull(m2.get("2"));
    m1.remove("2");
    assertNull(m2.get("2"));
    tx1.commit();
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    assertNull(m1.get("2"));
    m1.put("2", "World");
    m1.put("2", "Welt");
    tx1.rollback();
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    assertNull(m1.get("2"));
    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