views:

76

answers:

2

we experience with some regularity contention on a database table, and would like to evaluate a number of different options for resolving this issue.

in order to do so, i need to reproduce in a test case, contention on a table (any table) with repeatable reliability.

the approach i'm considering would be to reverse the semantics of a lock (e.g. java.util.concurrent.locks.ReentrantLock) and to release the lock when writing on the table begins, allowing all reads to occur at the time when the writing begins.

So therefore one writer thread holds the lock until shortly before doing an insert to a table, and then releasing the lock, multiple reader threads would attempt to run select statements against the same table.

Was wondering if there were any thoughts on such an approach, or if there is a simpler approach that could, with 100% reliability, reproduce contention on a db table.

thanks

+2  A: 

You could use a CountDownLatch with a count of 1.

final CountDownLatch barrier = new CountDownLatch(1);

You launch all of the reader threads, whose first action is

barrier.await();

then the writer thread can

barrier.countDown();

at which point all of the readers will merrily fire away.

Jonathan Feinberg
great idea! giving it a shot....
eqbridges
works like a charm :-)
eqbridges
Awesome. Glad to hear it.
Jonathan Feinberg
A: 

It depends a lot on your database as to how easy it is to produce contention in it. For example, if you are using Oracle, then doing a select will never produce any contention.

The easiest way to produce contention in the database is to do a select for read on the row that you know you are about to need to update.

Edit: After re-reading the question, I see that you seem to care more about "reader" contention on the database than update contention. The above idea can be used to force update contention, but not reader contention.

In the case, where you want to launch a mass number of readers to flood the database with selects, which should cause no actual contention, only starvation, then you can use the CountDownLatch as mentioned in another answer, or do it the old fashioned way with Object.wait/Object.notifyAll() if you are forced to run in a pre-1.5 JVM.

Edit 2: After reading the comment, probably the easiest way to emulate the contention that you are seeing is to use the Sybase lock table command. Just lock the table, fire up the selects, and then unlock the table. The selects should then all fire off... this also has the advantage of most accurately emulating the situation that you are trying to model.

Paul Wagland
thanks for your response! the contention we're seeing (in Sybase by the way) is when a huge insert is done to a very large table with a lot of indices on it. While that insert is happening, multiple selects are held in waiting while Sybase locks the whole table, and the index space as well.
eqbridges
that's an interesting approach, and one i'll look at. however, part of what we're trying to analyze is how changes to the writes will affect the contention. also, we're finding that often it uses row level locking but it locks the entire index so the write is fast but the txn is held up by the lock held while updating the indices.
eqbridges
Hmm... Just lost the upvote for adding more information to my answer?
Paul Wagland
Not sure what happened, when I upvoted it, i got this message "Vote too old to be changed, unless this answer is edited" and it reset to zero. Sorry about that!
eqbridges
Ah... not to worry. ;-)
Paul Wagland