FileDocCategorySizeDatePackage
TimestampLockingPolicy.javaAPI DocGlassfish v2 API13704Tue May 22 16:54:16 BST 2007oracle.toplink.essentials.descriptors

TimestampLockingPolicy

public class TimestampLockingPolicy extends VersionLockingPolicy

Purpose: Used to allow a single version timestamp to be used for optimistic locking.

since
TOPLink/Java 2.0

Fields Summary
protected int
retrieveTimeFrom
public static final int
SERVER_TIME
public static final int
LOCAL_TIME
Constructors Summary
public TimestampLockingPolicy()
PUBLIC: Create a new TimestampLockingPolicy. Defaults to using the time retrieved from the server.


                       
      
        super();
        this.useServerTime();
    
public TimestampLockingPolicy(String fieldName)
PUBLIC: Create a new TimestampLockingPolicy. Defaults to using the time retrieved from the server.

param
fieldName the field where the write lock value will be stored.

        super(fieldName);
        this.useServerTime();
    
public TimestampLockingPolicy(DatabaseField field)
INTERNAL: Create a new TimestampLockingPolicy. Defaults to using the time retrieved from the server.

param
field the field where the write lock value will be stored.

        super(field);
        this.useServerTime();
    
Methods Summary
public intcompareWriteLockValues(java.lang.Object value1, java.lang.Object value2)
INTERNAL: This method compares two writeLockValues. The writeLockValues should be non-null and of type java.sql.Timestamp. Returns: -1 if value1 is less (older) than value2; 0 if value1 equals value2; 1 if value1 is greater (newer) than value2. Throws: NullPointerException if the passed value is null; ClassCastException if the passed value is of a wrong type.

        java.sql.Timestamp timestampValue1 = (java.sql.Timestamp)value1;
        java.sql.Timestamp timestampValue2 = (java.sql.Timestamp)value2;
        return timestampValue1.compareTo(timestampValue2);
    
public java.lang.ObjectgetBaseValue()
INTERNAL: This is the base value that is older than all other values, it is used in the place of null in some situations.

        return new Timestamp(0);
    
protected java.lang.ClassgetDefaultLockingFieldType()
INTERNAL: Return the default timestamp locking filed java type, default is Timestamp.

        return ClassConstants.TIMESTAMP;
    
protected java.lang.ObjectgetInitialWriteValue(oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: returns the initial locking value

        if (usesLocalTime()) {
            return new Timestamp(System.currentTimeMillis());
        }
        if (usesServerTime()) {
            AbstractSession readSession = session.getSessionForClass(getDescriptor().getJavaClass());
            while (readSession.isUnitOfWork()) {
                readSession = ((UnitOfWorkImpl)readSession).getParent().getSessionForClass(getDescriptor().getJavaClass());
            }

            return readSession.getDatasourceLogin().getDatasourcePlatform().getTimestampFromServer(session, readSession.getName());
        }
        return null;

    
public java.lang.ObjectgetNewLockValue(oracle.toplink.essentials.queryframework.ModifyQuery query)
INTERNAL: Returns the new Timestamp value.

        return getInitialWriteValue(query.getSession());
    
public java.lang.ObjectgetValueToPutInCache(oracle.toplink.essentials.internal.sessions.AbstractRecord row, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Return the value that should be stored in the identity map. If the value is stored in the object, then return a null.

        if (isStoredInCache()) {
            return session.getDatasourcePlatform().convertObject(row.get(getWriteLockField()), ClassConstants.TIMESTAMP);
        } else {
            return null;
        }
    
public intgetVersionDifference(java.lang.Object currentValue, java.lang.Object domainObject, java.util.Vector primaryKeys, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Return the number of versions different between these objects.

        java.sql.Timestamp writeLockFieldValue;
        java.sql.Timestamp newWriteLockFieldValue = (java.sql.Timestamp)currentValue;
        if (newWriteLockFieldValue == null) {
            return 0;//merge it as either the object is new or being forced merged.
        }
        if (isStoredInCache()) {
            writeLockFieldValue = (java.sql.Timestamp)session.getIdentityMapAccessor().getWriteLockValue(primaryKeys, domainObject.getClass());
        } else {
            writeLockFieldValue = (java.sql.Timestamp)lockValueFromObject(domainObject);
        }
        if ((writeLockFieldValue != null) && !(newWriteLockFieldValue.after(writeLockFieldValue))) {
            return 0;
        }

        //if the new value is newer then perform the update.  Eventually this will be changed to
        //record the old version and compare that for equality
        return 2;
    
public oracle.toplink.essentials.expressions.ExpressiongetWriteLockUpdateExpression(oracle.toplink.essentials.expressions.ExpressionBuilder builder)
INTERNAL: Retrun an expression that updates the write lock

        return builder.value(getInitialWriteValue(builder.getSession()));
    
public java.lang.ObjectgetWriteLockValue(java.lang.Object domainObject, java.util.Vector primaryKey, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: This method will return the optimistic lock value for the object

        java.sql.Timestamp writeLockFieldValue = null;
        if (isStoredInCache()) {
            writeLockFieldValue = (java.sql.Timestamp)session.getIdentityMapAccessor().getWriteLockValue(primaryKey, domainObject.getClass());
        } else {
            //CR#2281 notStoredInCache prevent ClassCastException
            Object lockValue = lockValueFromObject(domainObject);
            if (lockValue != null) {
                if (lockValue instanceof java.sql.Timestamp) {
                    writeLockFieldValue = (java.sql.Timestamp)lockValueFromObject(domainObject);
                } else {
                    throw OptimisticLockException.needToMapJavaSqlTimestampWhenStoredInObject();
                }
            }
        }
        return writeLockFieldValue;
    
protected java.lang.NumberincrementWriteLockValue(java.lang.Number numberValue)
INTERNAL: Timestamp versioning should not be able to do this. Override the superclass behaviour.

        return null;
    
public booleanisChildWriteLockValueGreater(oracle.toplink.essentials.internal.sessions.AbstractSession session, java.util.Vector primaryKey, java.lang.Class original, oracle.toplink.essentials.internal.sessions.ObjectChangeSet changeSet)
INTERNAL: Update the parent write lock value if the objectChangeSet's is greater.

        if (isStoredInCache()) {
            // If this uow changed the object the version must be updated,
            // we can check this by ensuring our value is greater than our parent's.
            java.sql.Timestamp writeLockValue = (java.sql.Timestamp)changeSet.getWriteLockValue();
            java.sql.Timestamp parentValue = (java.sql.Timestamp)session.getIdentityMapAccessor().getWriteLockValue(primaryKey, original);
            if (writeLockValue != null) {// This occurs if the object was deleted
                if ((parentValue == null) || parentValue.before(writeLockValue)) {// Check parent value is less than child
                    return true;
                }
            }
        }

        return false;
    
public booleanisChildWriteLockValueGreater(oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl uow, java.util.Vector primaryKey, java.lang.Class original)
INTERNAL: Update the parent write lock value if the unit of works has been incremented.

        if (isStoredInCache()) {
            // If this uow changed the object the version must be updated,
            // we can check this by ensuring our value is greater than our parent's.
            java.sql.Timestamp writeLockValue = (java.sql.Timestamp)uow.getIdentityMapAccessor().getWriteLockValue(primaryKey, original);
            java.sql.Timestamp parentValue = (java.sql.Timestamp)uow.getParent().getIdentityMapAccessor().getWriteLockValue(primaryKey, original);
            if (writeLockValue != null) {// This occurs if the object was deleted
                if ((parentValue == null) || parentValue.before(writeLockValue)) {// Check parent value is less than child
                    return true;
                }
            }
        }

        return false;
    
public booleanisNewerVersion(java.lang.Object currentValue, java.lang.Object domainObject, java.util.Vector primaryKey, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Compares the value with the value from the object (or cache). Will return true if the object is newer.

        java.sql.Timestamp writeLockFieldValue;
        java.sql.Timestamp newWriteLockFieldValue = (java.sql.Timestamp)currentValue;
        if (isStoredInCache()) {
            writeLockFieldValue = (java.sql.Timestamp)session.getIdentityMapAccessor().getWriteLockValue(primaryKey, domainObject.getClass());
        } else {
            writeLockFieldValue = (java.sql.Timestamp)lockValueFromObject(domainObject);
        }
        if ((writeLockFieldValue != null) && !(newWriteLockFieldValue.after(writeLockFieldValue))) {
            return false;
        }
        return true;
    
public booleanisNewerVersion(oracle.toplink.essentials.internal.sessions.AbstractRecord databaseRow, java.lang.Object domainObject, java.util.Vector primaryKey, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Compares the value from the row and from the object (or cache). Will return true if the object is newer than the row.

        java.sql.Timestamp writeLockFieldValue;
        java.sql.Timestamp newWriteLockFieldValue = (java.sql.Timestamp)session.getDatasourcePlatform().convertObject(databaseRow.get(getWriteLockField()), ClassConstants.TIMESTAMP);
        if (isStoredInCache()) {
            writeLockFieldValue = (java.sql.Timestamp)session.getIdentityMapAccessor().getWriteLockValue(primaryKey, domainObject.getClass());
        } else {
            writeLockFieldValue = (java.sql.Timestamp)lockValueFromObject(domainObject);
        }
        if ((writeLockFieldValue != null) && !(newWriteLockFieldValue.after(writeLockFieldValue))) {
            return false;
        }
        return true;
    
public voidsetUsesServerTime(boolean usesServerTime)
PUBLIC: Set if policy uses server time.

        if (usesServerTime) {
            useServerTime();
        } else {
            useLocalTime();
        }
    
public voiduseLocalTime()
PUBLIC: set this policy to get the time from the local machine.

        retrieveTimeFrom = LOCAL_TIME;
    
public voiduseServerTime()
PUBLIC: set this policy to get the time from the server.

        retrieveTimeFrom = SERVER_TIME;
    
public booleanusesLocalTime()
PUBLIC: Return true if policy uses local time.

        return (retrieveTimeFrom == LOCAL_TIME);
    
public booleanusesServerTime()
PUBLIC: Return true if policy uses server time.

        return (retrieveTimeFrom == SERVER_TIME);