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);
}
}
}
}
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);
}
}
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();
}
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();
}
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();
}
Aggregations