// helper thread. The main thread uses it to tell the helper thread
// that it has started the scan. The helper thread uses it
// to tell the main thread that it is ready.
// Both threads should wait until the other thread
// has reached the barrier before continuing.
final Barrier barrier = new Barrier(2);
// start the second thread and make it do the same update
new AsyncThread(new AsyncTask() {
public void doWork(Connection conn) throws Exception {
conn.setAutoCommit(false);
Statement s = conn.createStatement();
// note: asserts in this inner class do not make the test fail
// so, just executing it here
s.executeUpdate("update account set b = b + 11");
s.close();
// Tell the main thread that we've locked the row
barrier.await();
// The main thread now can continue - give it a
// second to do its stuff
//Thread.sleep(1000L);
// we check that the 'wait' state is gone at the main thread,
// it would not cause the test to fail if we checked it here.
}
});
// now select from syscs_diag.lock_table, don't wait more than minute.
int totalWait = 0;
boolean found=false;
do {
totalWait += 500;
Thread.sleep(500);
// we want to look for 'WAIT' state. There will also
// be one of more 'GRANT' state locks, likely background threads,
// but we're not interested in those here.
found=getWaitState();
} while (!found && totalWait < 6000);
// defer the assert until we've alerted the async thread
// commit will release the lock
commit();
// set the timeout back so things can timeout.
s.executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY" +
"('derby.locks.waitTimeout','5')");
commit();
// Now that we've found the wait state, tell the helper thread we
// are done
barrier.await();
// now that we've released the helper thread, we can safely let
// the test fail if the results of the earlier check were bad
assertTrue("expected to find a 'WAIT' state, but did not", found);