Here is a test harness that proves that you only get row level locking
when you use MVCC.  If you take MVCC out of the connection string then
the second thread waits for the first one to commit.  Leave it in and
you get both of the threads inserting with no delay.


public class RowLevelLockingTestHarness {

        public static void main(String[] args) throws Exception{
        final DataSource ds = new SimpleDriverDataSource(new
org.h2.Driver(), "jdbc:h2:mem:test_db;DB_CLOSE_DELAY=100;MVCC=TRUE",
"sa", "");

        ds.getConnection().createStatement().execute("create table TEST
(ID integer primary key)");
        ds.getConnection().createStatement().execute("SET
DEFAULT_LOCK_TIMEOUT 50000");

        new LockingTest(ds, 1).start();
        new LockingTest(ds, 2).start();

        Thread.sleep(1000 * 1000);
        }

    static class LockingTest extends Thread {
        DataSource ds;
                private int rowId;
        LockingTest(DataSource ds, int rowId) {
                setName("Insert " + rowId);
                this.ds = ds;
                this.rowId = rowId;
        }
                @Override
                public void run() {
                        try {
                                Connection c1 = ds.getConnection();
                                c1.setAutoCommit(false);
                                log("  About to insert " + rowId);
                                c1.createStatement().execute("insert into TEST 
(ID) values (" +
rowId + ")");
                                log("  Inserted " + rowId);

                                Thread.sleep(10 * 1000);
                                log("  Committing");
                                c1.commit();
                                log("  Commit complete");
                        }
                        catch (Exception e) {
                                e.printStackTrace();
                        }
                }

    }

    static void log(String str) {
 
System.out.println(DateFormat.getTimeInstance(DateFormat.MEDIUM).format(new
        Date()) + "  " +
                Thread.currentThread().getName() + "  " +
                str);
    }

}

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To post to this group, send email to h2-datab...@googlegroups.com.
To unsubscribe from this group, send email to 
h2-database+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

Reply via email to