Side of Software
Persistence Library 2.0

sos.db
Class DefaultReadWriteTransactionPolicy

java.lang.Object
  extended by sos.db.AbstractTransactionPolicy
      extended by sos.db.DefaultReadWriteTransactionPolicy
All Implemented Interfaces:
java.io.Serializable, TransactionPolicy

public class DefaultReadWriteTransactionPolicy
extends AbstractTransactionPolicy
implements TransactionPolicy

A transaction policy that uses strict two phase locking (2PL) to ensure that transactions appear to execute sequentially and that uses lock timeouts to break potential deadlock.

With this policy, transactions on different objects are allowed to execute concurrently. Also, transactions invoking read-only methods on the same objects are allowed to execute concurrently.

With this policy, transactions in high contention may deadlock, and repeated attempts at these transactions may lead to thrashing, causing no transaction to make progress. To avoid the thrashing, consider using DefaultTransactionPolicy instead of this class.

This policy is adequate for applications in which transactions execute on different threads and without much contention.

Logging

This class logs fine-grained activity, as given by the following table:

ActivitySeverity
A read lock is acquiredFINEST
A read lock is releasedFINEST
A write lock is acquiredFINEST
A write lock is releasedFINEST
A method is invokedFINEST

Subclasses may change the log messages or severity by overriding the logXxx methods.

Since:
1.0
See Also:
DefaultTransactionPolicy

Constructor Summary
DefaultReadWriteTransactionPolicy()
          Creates an instance of DefaultReadWriteTransactionPolicy with default timeout intervals of one minute each.
DefaultReadWriteTransactionPolicy(long readTimeout, long writeTimeout)
          Creates an instance of DefaultReadWriteTransactionPolicy with the specified timeout intervals.
 
Method Summary
protected  void acquireReadLock(TransactionSupport support)
          Acquires a read lock for the key referenced by support.
protected  void acquireWriteLock(TransactionSupport support)
          Acquires a write lock for the key referenced by support.
protected  void logReadLockAcquired(java.lang.String databaseName, java.lang.Object key)
          Records that the read lock associated with key has been acquired.
protected  void logReadLockReleased(java.lang.String databaseName, java.lang.Object key)
          Records that the read lock associated with key has been released.
protected  void logWriteLockAcquired(java.lang.String databaseName, java.lang.Object key)
          Records that the write lock associated with key has been acquired.
protected  void logWriteLockReleased(java.lang.String databaseName, java.lang.Object key)
          Records that the write lock associated with key has been released.
protected  void prepareForReadOnlyInvocation(TransactionSupport support)
          Prepares for the invocation of a read-only method on the object referenced by support.
protected  void prepareForWriteInvocation(TransactionSupport support)
          Prepares for the invocation of a non-read-only method on the object referenced by support.
 void registerObject(TransactionSupport support, java.lang.Object object)
          Notifies this policy that there is a new database object.
 
Methods inherited from class sos.db.AbstractTransactionPolicy
getMethodStatus, invoke, logMethodInvocation, startTransaction, unregisterObjects
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface sos.db.TransactionPolicy
invoke, startTransaction, unregisterObjects
 

Constructor Detail

DefaultReadWriteTransactionPolicy

public DefaultReadWriteTransactionPolicy()
Creates an instance of DefaultReadWriteTransactionPolicy with default timeout intervals of one minute each.

See Also:
DefaultReadWriteTransactionPolicy(long,long)

DefaultReadWriteTransactionPolicy

public DefaultReadWriteTransactionPolicy(long readTimeout,
                                         long writeTimeout)
Creates an instance of DefaultReadWriteTransactionPolicy with the specified timeout intervals.

Parameters:
readTimeout - maximum time to wait (in milliseconds) for a read lock
writeTimeout - maximum time to wait (in milliseconds) for a write lock
Method Detail

acquireReadLock

protected void acquireReadLock(TransactionSupport support)
                        throws java.io.IOException
Acquires a read lock for the key referenced by support.

Parameters:
support - object that holds the key for which to acquire the read lock
Throws:
java.io.IOException - if an I/O error occurs

acquireWriteLock

protected void acquireWriteLock(TransactionSupport support)
                         throws java.io.IOException
Acquires a write lock for the key referenced by support.

Parameters:
support - object that holds the key for which to acquire the write lock
Throws:
java.io.IOException - if an I/O error occurs

logReadLockAcquired

protected void logReadLockAcquired(java.lang.String databaseName,
                                   java.lang.Object key)
Records that the read lock associated with key has been acquired.

It is safe to invoke toString on key.

This implementation uses the localized message for key "logReadLockAcquired". The message takes three parameters: the database name, the transaction thread, and the key.

Parameters:
databaseName - name of the database
key - unique key of object being read

logReadLockReleased

protected void logReadLockReleased(java.lang.String databaseName,
                                   java.lang.Object key)
Records that the read lock associated with key has been released.

It is safe to invoke toString on key.

This implementation uses the localized message for key "logReadLockReleased". The message takes three parameters: the database name, the transaction thread, and the key.

Parameters:
databaseName - name of the database
key - unique key of object being read

logWriteLockAcquired

protected void logWriteLockAcquired(java.lang.String databaseName,
                                    java.lang.Object key)
Records that the write lock associated with key has been acquired.

It is safe to invoke toString on key.

This implementation uses the localized message for key "logWriteLockAcquired". The message takes three parameters: the database name, the transaction thread, and the key.

Parameters:
databaseName - name of the database
key - unique key of object being written

logWriteLockReleased

protected void logWriteLockReleased(java.lang.String databaseName,
                                    java.lang.Object key)
Records that the write lock associated with key has been released.

It is safe to invoke toString on key.

This implementation uses the localized message for key "logWriteLockReleased". The message takes three parameters: the database name, the transaction thread, and the key.

Parameters:
databaseName - name of the database
key - unique key of object being written

prepareForReadOnlyInvocation

protected void prepareForReadOnlyInvocation(TransactionSupport support)
                                     throws java.io.IOException
Description copied from class: AbstractTransactionPolicy
Prepares for the invocation of a read-only method on the object referenced by support.

This implementation does nothing. Subclasses can override this method to provide a more meaningful implementation. For example, they can use this method to acquire a read lock.

Overrides:
prepareForReadOnlyInvocation in class AbstractTransactionPolicy
Parameters:
support - object that holds information about the receiver object, including a key that uniquely identifies it
Throws:
java.io.IOException - if an I/O error occurs
See Also:
AbstractTransactionPolicy.prepareForWriteInvocation(sos.db.TransactionSupport)

prepareForWriteInvocation

protected void prepareForWriteInvocation(TransactionSupport support)
                                  throws java.io.IOException
Description copied from class: AbstractTransactionPolicy
Prepares for the invocation of a non-read-only method on the object referenced by support.

This implementation does nothing. Subclasses can override this method to provide a more meaningful implementation. For example, they can use this method to acquire a write lock.

Overrides:
prepareForWriteInvocation in class AbstractTransactionPolicy
Parameters:
support - object that holds information about the receiver object, including a key that uniquely identifies it
Throws:
java.io.IOException - if an I/O error occurs
See Also:
AbstractTransactionPolicy.prepareForReadOnlyInvocation(sos.db.TransactionSupport)

registerObject

public void registerObject(TransactionSupport support,
                           java.lang.Object object)
                    throws java.io.IOException
Description copied from interface: TransactionPolicy
Notifies this policy that there is a new database object. The database calls this method when an object is added to the database.

Specified by:
registerObject in interface TransactionPolicy
Overrides:
registerObject in class AbstractTransactionPolicy
Parameters:
support - object to assist in transaction management
object - the object added to the database
Throws:
java.io.IOException - if an I/O error occurs

Side of Software
Persistence Library 2.0

Copyright 2004-08 Side of Software (SOS). All rights reserved.