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