Transactions


SonicBase supports the read committed transaction isolation level.

Simple Example


conn.setAutoCommit(false);

PreparedStatement stmt = conn.prepareStatement("insert into persons (id) VALUES (?)");
stmt.setLong(1, 100000); int count = stmt.executeUpdate(); assertEquals(count, 1);
conn.commit();

Concurrent Access Example


PreparedStatement stmt = conn.prepareStatement("truncate table persons");
stmt.execute();

conn.setAutoCommit(false);
stmt = conn.prepareStatement("insert into persons (id, id2, socialSecurityNumber, relatives, restricted, gender) VALUES (?, ?, ?, ?, ?, ?)"); stmt.setLong(1, 100); stmt.setLong(2, (100) % 2); stmt.setString(3, "933-28-" + (4)); stmt.setString(4, "12345678901,12345678901|12345678901,12345678901,12345678901,12345678901|12345678901"); stmt.setBoolean(5, false); stmt.setString(6, "m"); int count = stmt.executeUpdate(); assertEquals(count, 1);
final CountDownLatch latch = new CountDownLatch(1); Thread thread = new Thread(new Runnable() { @Override public void run() { try { PreparedStatement stmt = conn2.prepareStatement("update persons set id = ?, socialSecurityNumber=? where id=?"); stmt.setLong(1, 2000); stmt.setString(2, "ssn"); stmt.setLong(3, 100); int count = stmt.executeUpdate(); assertEquals(count, 0); // record not found } catch (Exception e) { e.printStackTrace(); } finally { latch.countDown(); } } }); thread.start();
latch.await();
stmt = conn.prepareStatement("update persons set id = ?, socialSecurityNumber=? where id=?"); stmt.setLong(1, 1000); stmt.setString(2, "ssn"); stmt.setLong(3, 100); count = stmt.executeUpdate(); assertEquals(count, 1);
conn.commit();
stmt = conn.prepareStatement("select * from persons where id=1000"); ResultSet resultSet = stmt.executeQuery(); assertTrue(resultSet.next()); assertEquals(resultSet.getLong("id"), 1000); assertEquals(resultSet.getString("socialsecuritynumber"), "ssn");