QJCC homepage

biz.chitec.quarterback.util
Class RowLock

java.lang.Object
  extended bybiz.chitec.quarterback.util.RowLock

public final class RowLock
extends java.lang.Object

Class to lock unique rows of tables against concurrent access using a database that doesn't support this on its own. This class consists only of static methods that operate on a JDBC-Statement to access a table "rowlocktable". In MySQL, that table would have to be created like this:

create table rowlocktable(
  tablename char(40) not null,
  rownr int not null,
  unique(tablename,rownr)
);
The class itself implements all element functions that should be implemented to operate with locks.

Note that this class cannot be instantiated.

Version:
$Id: f59b9dcdb004d1de08652614e56f0df50e950d3b $
Author:
Dirk Hillbrecht 1997-1999, chitec/Dirk Hillbrecht 2000 Distributed under the terms of the GNU LGPL.

Field Summary
private static java.util.Random rndgen
           
static java.lang.String TABLENAME
           
 
Constructor Summary
private RowLock()
           
 
Method Summary
static int checkIntegrity(java.sql.Statement stmt)
          Checks and restores the integrity of the locks table.
static void lock(java.sql.Statement stmt, java.lang.String table)
          Obtain monitor for complete table and always wait for semaphore.
static void lock(java.sql.Statement stmt, java.lang.String table, boolean waitforsemaphore)
          Obtain monitor for complete table and wait for semaphore if said so.
static void lock(java.sql.Statement stmt, java.lang.String table, int row)
          Aquires semaphore and waits for it.
static void lock(java.sql.Statement stmt, java.lang.String table, int row, boolean waitforsemaphore)
          Main implementation of semaphone aquisition.
static void lock(java.sql.Statement stmt, java.lang.String table, int row, long waitmillis)
          Main implementation of semaphone aquisition.
static void lock(java.sql.Statement stmt, java.lang.String table, long waitmillis)
          Obtain monitor for complete table and wait for semaphore if said so.
static void unlock(java.sql.Statement stmt, java.lang.String table)
          Releases table-wide monitor.
static void unlock(java.sql.Statement stmt, java.lang.String table, int row)
          Releases monitor for row in table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TABLENAME

public static final java.lang.String TABLENAME
See Also:
Constant Field Values

rndgen

private static final java.util.Random rndgen
Constructor Detail

RowLock

private RowLock()
Method Detail

checkIntegrity

public static final int checkIntegrity(java.sql.Statement stmt)
                                throws java.sql.SQLException
Checks and restores the integrity of the locks table. Returns the number of generators whose table entries had to be repaired.

Throws:
java.sql.SQLException

lock

public static final void lock(java.sql.Statement stmt,
                              java.lang.String table,
                              int row,
                              long waitmillis)
                       throws java.sql.SQLException
Main implementation of semaphone aquisition. Aquires the required semaphore. If waitmillis is less than 1, it will only once try to fetch the semaphore. If it fails, it throws a ConcurrentModificationException. Otherwise, it will wait up to as many milliseconds as given (at most 300 milliseconds more) to obtain the lock and throw the ConcurrentModificationExceptoin afterwards. If waitmillis is set to Long.MAX_VALUE, the method will wait indefinitely which could lead to a deadlock.

Parameters:
stmt - Statement to work on the database with
table - Name of table to aquire lock for. Just a general key.
row - Row in the table to obtain lock for. Can be -1 for general locks.
waitmillis - Maximum time to wait in milliseconds to catch the semaphore. Actual waiting can be up to 300 milliseconds more.
Throws:
java.util.ConcurrentModificationException - If semaphore could not be aquired
java.sql.SQLException

lock

public static final void lock(java.sql.Statement stmt,
                              java.lang.String table,
                              int row,
                              boolean waitforsemaphore)
                       throws java.sql.SQLException
Main implementation of semaphone aquisition. Aquires the required semaphore. If waitforsemaphore is set to true, this method will only return after it could aquire it. This can lead to deadlocks. If waitforsemaphore is set to false, it will only once try to fetch the semaphore. If it fails, it throws a ConcurrentModificationException.

Parameters:
stmt - Statement to work on the database with
table - Name of table to aquire lock for. Just a general key.
row - Row in the table to obtain lock for. Can be -1 for general locks.
waitforsemaphore - Flag whether method should wait for semaphore of throw an exception if aquisition fails
Throws:
java.util.ConcurrentModificationException - If semaphore could not be aquired
java.sql.SQLException

lock

public static final void lock(java.sql.Statement stmt,
                              java.lang.String table,
                              int row)
                       throws java.sql.SQLException
Aquires semaphore and waits for it. This method will never throw a ConcurrentModificationException. Instead, it will wait for the semaphore until it is free.

Parameters:
stmt - Statement to work on the database with
table - Name of table to aquire lock for. Just a general key.
row - Row in the table to obtain lock for. Can be -1 for general locks.
Throws:
java.sql.SQLException

unlock

public static final void unlock(java.sql.Statement stmt,
                                java.lang.String table,
                                int row)
                         throws java.sql.SQLException
Releases monitor for row in table.

Throws:
java.sql.SQLException

lock

public static final void lock(java.sql.Statement stmt,
                              java.lang.String table,
                              boolean waitforsemaphore)
                       throws java.sql.SQLException
Obtain monitor for complete table and wait for semaphore if said so. Note that table monitors do not depend in any way on monitors for specific rows of the same table. Each of them can be total independently aquired from each other.

Throws:
java.sql.SQLException

lock

public static final void lock(java.sql.Statement stmt,
                              java.lang.String table,
                              long waitmillis)
                       throws java.sql.SQLException
Obtain monitor for complete table and wait for semaphore if said so. Note that table monitors do not depend in any way on monitors for specific rows of the same table. Each of them can be total independently aquired from each other.

Throws:
java.sql.SQLException

lock

public static final void lock(java.sql.Statement stmt,
                              java.lang.String table)
                       throws java.sql.SQLException
Obtain monitor for complete table and always wait for semaphore. Note that table monitors do not depend in any way on monitors for specific rows of the same table. Each of them can be total independently aquired from each other.

Throws:
java.sql.SQLException

unlock

public static final void unlock(java.sql.Statement stmt,
                                java.lang.String table)
                         throws java.sql.SQLException
Releases table-wide monitor.

Throws:
java.sql.SQLException

QJCC homepage