use of org.h2.test.synth.TestConcurrentUpdate in project h2database by h2database.
the class TestTransactionStore method testConcurrentUpdate.
private void testConcurrentUpdate() {
MVStore s;
TransactionStore ts;
s = MVStore.open(null);
ts = new TransactionStore(s);
ts.init();
Transaction tx1 = ts.begin();
TransactionMap<Integer, Integer> map1 = tx1.openMap("data");
map1.put(1, 10);
Transaction tx2 = ts.begin();
TransactionMap<Integer, Integer> map2 = tx2.openMap("data");
try {
map2.put(1, 20);
fail();
} catch (IllegalStateException e) {
assertEquals(DataUtils.ERROR_TRANSACTION_LOCKED, DataUtils.getErrorCode(e.getMessage()));
}
assertEquals(10, map1.get(1).intValue());
assertNull(map2.get(1));
tx1.commit();
assertEquals(10, map2.get(1).intValue());
s.close();
}
use of org.h2.test.synth.TestConcurrentUpdate in project h2database by h2database.
the class TestMvcc2 method testConcurrentUpdate.
private void testConcurrentUpdate() throws Exception {
Connection conn = getConnection();
final Connection conn2 = getConnection();
Statement stat = conn.createStatement();
final Statement stat2 = conn2.createStatement();
stat2.execute("set lock_timeout 1000");
stat.execute("create table test(id int primary key, name varchar)");
stat.execute("insert into test values(0, 'Hello')");
conn.setAutoCommit(false);
Task t = new Task() {
@Override
public void call() throws SQLException {
stat2.execute("update test set name = 'Hallo'");
}
};
stat.execute("update test set name = 'Hi'");
t.execute();
Thread.sleep(500);
conn.commit();
t.get();
ResultSet rs;
rs = stat.executeQuery("select name from test");
rs.next();
assertEquals("Hallo", rs.getString(1));
stat.execute("drop table test");
conn2.close();
conn.close();
}
use of org.h2.test.synth.TestConcurrentUpdate in project h2database by h2database.
the class TestMultiThread method testConcurrentUpdate.
private void testConcurrentUpdate() throws Exception {
deleteDb("lockMode");
final int objectCount = 10000;
final String url = getURL("lockMode;MULTI_THREADED=1;LOCK_TIMEOUT=10000", true);
int threadCount = 25;
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
Connection conn = getConnection(url);
try {
conn.createStatement().execute("CREATE TABLE IF NOT EXISTS ACCOUNT" + "(ID NUMBER(18,0) not null PRIMARY KEY, BALANCE NUMBER null)");
final PreparedStatement mergeAcctStmt = conn.prepareStatement("MERGE INTO Account(id, balance) key (id) VALUES (?, ?)");
for (int i = 0; i < objectCount; i++) {
mergeAcctStmt.setLong(1, i);
mergeAcctStmt.setBigDecimal(2, BigDecimal.ZERO);
mergeAcctStmt.execute();
}
final ArrayList<Callable<Void>> callables = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
callables.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
try (Connection taskConn = getConnection(url)) {
taskConn.setAutoCommit(false);
final PreparedStatement updateAcctStmt = taskConn.prepareStatement("UPDATE account SET balance = ? WHERE id = ?");
for (int j = 0; j < 1000; j++) {
updateAcctStmt.setDouble(1, Math.random());
updateAcctStmt.setLong(2, (int) (Math.random() * objectCount));
updateAcctStmt.execute();
taskConn.commit();
}
}
return null;
}
});
}
final ArrayList<Future<Void>> jobs = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
jobs.add(executor.submit(callables.get(i)));
}
// check for exceptions
for (Future<Void> job : jobs) {
job.get(5, TimeUnit.MINUTES);
}
} finally {
IOUtils.closeSilently(conn);
executor.shutdown();
executor.awaitTermination(20, TimeUnit.SECONDS);
}
deleteDb("lockMode");
}
use of org.h2.test.synth.TestConcurrentUpdate in project h2database by h2database.
the class TestAll method test.
/**
* Run all tests with the current settings.
*/
private void test() throws SQLException {
System.out.println();
System.out.println("Test " + toString() + " (" + Utils.getMemoryUsed() + " KB used)");
beforeTest();
// db
addTest(new TestScriptSimple());
addTest(new TestScript());
addTest(new TestAlter());
addTest(new TestAlterSchemaRename());
addTest(new TestAutoRecompile());
addTest(new TestBackup());
addTest(new TestBigDb());
addTest(new TestBigResult());
addTest(new TestCases());
addTest(new TestCheckpoint());
addTest(new TestCompatibility());
addTest(new TestCompatibilityOracle());
addTest(new TestCsv());
addTest(new TestDeadlock());
if (vmlens) {
return;
}
addTest(new TestDrop());
addTest(new TestDuplicateKeyUpdate());
addTest(new TestEncryptedDb());
addTest(new TestExclusive());
addTest(new TestFullText());
addTest(new TestFunctionOverload());
addTest(new TestFunctions());
addTest(new TestInit());
addTest(new TestIndex());
addTest(new TestIndexHints());
addTest(new TestLargeBlob());
addTest(new TestLinkedTable());
addTest(new TestListener());
addTest(new TestLob());
addTest(new TestMergeUsing());
addTest(new TestMultiConn());
addTest(new TestMultiDimension());
addTest(new TestMultiThreadedKernel());
addTest(new TestOpenClose());
addTest(new TestOptimizations());
addTest(new TestOptimizerHints());
addTest(new TestOutOfMemory());
addTest(new TestReadOnly());
addTest(new TestRecursiveQueries());
addTest(new TestGeneralCommonTableQueries());
if (!memory) {
// requires persistent store for reconnection tests
addTest(new TestPersistentCommonTableExpressions());
}
addTest(new TestRights());
addTest(new TestRunscript());
addTest(new TestSQLInjection());
addTest(new TestSessionsLocks());
addTest(new TestSelectCountNonNullColumn());
addTest(new TestSequence());
addTest(new TestShow());
addTest(new TestSpaceReuse());
addTest(new TestSpatial());
addTest(new TestSpeed());
addTest(new TestTableEngines());
addTest(new TestRowFactory());
addTest(new TestTempTables());
addTest(new TestTransaction());
addTest(new TestTriggersConstraints());
addTest(new TestTwoPhaseCommit());
addTest(new TestView());
addTest(new TestViewAlterTable());
addTest(new TestViewDropView());
addTest(new TestReplace());
addTest(new TestSynonymForTable());
addTest(new TestColumnNamer());
// jaqu
addTest(new AliasMapTest());
addTest(new AnnotationsTest());
addTest(new ClobTest());
addTest(new ModelsTest());
addTest(new SamplesTest());
addTest(new UpdateTest());
// jdbc
addTest(new TestBatchUpdates());
addTest(new TestCallableStatement());
addTest(new TestCancel());
addTest(new TestConcurrentConnectionUsage());
addTest(new TestConnection());
addTest(new TestDatabaseEventListener());
addTest(new TestJavaObject());
addTest(new TestLimitUpdates());
addTest(new TestLobApi());
addTest(new TestManyJdbcObjects());
addTest(new TestMetaData());
addTest(new TestNativeSQL());
addTest(new TestPreparedStatement());
addTest(new TestResultSet());
addTest(new TestStatement());
addTest(new TestGetGeneratedKeys());
addTest(new TestTransactionIsolation());
addTest(new TestUpdatableResultSet());
addTest(new TestZloty());
addTest(new TestCustomDataTypesHandler());
addTest(new TestSetCollation());
// jdbcx
addTest(new TestConnectionPool());
addTest(new TestDataSource());
addTest(new TestXA());
addTest(new TestXASimple());
// server
addTest(new TestAutoServer());
addTest(new TestNestedLoop());
// mvcc & row level locking
addTest(new TestMvcc1());
addTest(new TestMvcc2());
addTest(new TestMvcc3());
addTest(new TestMvcc4());
addTest(new TestMvccMultiThreaded());
addTest(new TestMvccMultiThreaded2());
addTest(new TestRowLocks());
// synth
addTest(new TestBtreeIndex());
addTest(new TestConcurrentUpdate());
addTest(new TestDiskFull());
addTest(new TestCrashAPI());
addTest(new TestFuzzOptimizations());
addTest(new TestLimit());
addTest(new TestRandomCompare());
addTest(new TestKillRestart());
addTest(new TestKillRestartMulti());
addTest(new TestMultiThreaded());
addTest(new TestOuterJoins());
addTest(new TestNestedJoins());
addTest(new TestStringAggCompatibility());
runAddedTests();
// serial
addTest(new TestDateStorage());
addTest(new TestDriver());
addTest(new TestJavaObjectSerializer());
addTest(new TestLocale());
addTest(new TestMemoryUsage());
addTest(new TestMultiThread());
addTest(new TestPowerOff());
addTest(new TestReorderWrites());
addTest(new TestRandomSQL());
addTest(new TestQueryCache());
addTest(new TestUrlJavaObjectSerializer());
addTest(new TestWeb());
runAddedTests(1);
afterTest();
}
use of org.h2.test.synth.TestConcurrentUpdate in project h2database by h2database.
the class TestMvccMultiThreaded method testConcurrentUpdate.
private void testConcurrentUpdate() throws Exception {
deleteDb(getTestName());
int len = 2;
final Connection[] connList = new Connection[len];
for (int i = 0; i < len; i++) {
connList[i] = getConnection(getTestName() + ";MVCC=TRUE");
}
Connection conn = connList[0];
conn.createStatement().execute("create table test(id int primary key, value int)");
conn.createStatement().execute("insert into test values(0, 0)");
final int count = 1000;
Task[] tasks = new Task[len];
final CountDownLatch latch = new CountDownLatch(len);
for (int i = 0; i < len; i++) {
final int x = i;
tasks[i] = new Task() {
@Override
public void call() throws Exception {
for (int a = 0; a < count; a++) {
connList[x].createStatement().execute("update test set value=value+1");
latch.countDown();
latch.await();
}
}
};
tasks[i].execute();
}
for (int i = 0; i < len; i++) {
tasks[i].get();
}
ResultSet rs = conn.createStatement().executeQuery("select value from test");
rs.next();
assertEquals(count * len, rs.getInt(1));
for (int i = 0; i < len; i++) {
connList[i].close();
}
}
Aggregations