/** * PUBLIC: * Set the locking policy to use timestamp version locking. * This updates the timestamp field on all updates, first comparing that the field has not changed to detect locking conflicts. * Note: many database have limited precision of timestamps which can be an issue is highly concurrent systems. * * The parameter 'shouldStoreInCache' configures the version lock value to be stored in the cache or in the object. * Note: if using a stateless model where the object can be passed to a client and then later updated in a different transaction context, * then the version lock value should not be stored in the cache, but in the object to ensure it is the correct value for that object. * @see VersionLockingPolicy */ public void useTimestampLocking(String writeLockFieldName, boolean shouldStoreInCache) { TimestampLockingPolicy policy = new TimestampLockingPolicy(writeLockFieldName); if (shouldStoreInCache) { policy.storeInCache(); } else { policy.storeInObject(); } setOptimisticLockingPolicy(policy); }
/** * PUBLIC: * Set if policy uses server time. */ public void setUsesServerTime(boolean usesServerTime) { if (usesServerTime) { useServerTime(); } else { useLocalTime(); } }
/** * INTERNAL: * This method will return the optimistic lock value for the object */ public Object getWriteLockValue(Object domainObject, java.util.Vector primaryKey, AbstractSession session) { 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; }
/** * INTERNAL: * returns the initial locking value */ protected Object getInitialWriteValue(AbstractSession session) { 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; }
/** * INTERNAL: * Compares the value from the row and from the object (or cache). * Will return true if the object is newer than the row. */ public boolean isNewerVersion(AbstractRecord databaseRow, Object domainObject, java.util.Vector primaryKey, AbstractSession session) { 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); } // bug 6342382: object's lock value is null, it is NOT newer than any newWriteLockFieldValue. if(writeLockFieldValue == null) { return false; } // 2.5.1.6 if the write lock value is null, then what ever we have is treated as newer. if (newWriteLockFieldValue == null) { return true; } if (!(newWriteLockFieldValue.after(writeLockFieldValue))) { return false; } return true; }
/** * INTERNAL: * Return the value that should be stored in the identity map. If the value * is stored in the object, then return a null. */ public Object getValueToPutInCache(AbstractRecord row, AbstractSession session) { if (isStoredInCache()) { return session.getDatasourcePlatform().convertObject(row.get(getWriteLockField()), ClassConstants.TIMESTAMP); } else { return null; } }
/** * 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. */ public TimestampLockingPolicy(String fieldName) { super(fieldName); this.useServerTime(); }
/** * INTERNAL: * Returns the new Timestamp value. */ public Object getNewLockValue(ModifyQuery query) { return getInitialWriteValue(query.getSession()); }
/** * INTERNAL: * Update the parent write lock value if the objectChangeSet's is greater. */ public boolean isChildWriteLockValueGreater(AbstractSession session, java.util.Vector primaryKey, Class original, ObjectChangeSet changeSet) { 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; }
/** * INTERNAL: */ public void useTimestampLockingPolicy(DatabaseField field) { useVersionLockingPolicy(new TimestampLockingPolicy(field)); }
/** * INTERNAL: * Return the number of versions different between these objects. */ public int getVersionDifference(Object currentValue, Object domainObject, Vector primaryKeys, AbstractSession session) { 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; }
/** * 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. */ public TimestampLockingPolicy(DatabaseField field) { super(field); this.useServerTime(); }
/** * INTERNAL: * Retrun an expression that updates the write lock */ public Expression getWriteLockUpdateExpression(ExpressionBuilder builder) { return builder.value(getInitialWriteValue(builder.getSession())); }
/** * INTERNAL: * Update the parent write lock value if the unit of works has been incremented. */ public boolean isChildWriteLockValueGreater(UnitOfWorkImpl uow, java.util.Vector primaryKey, Class original) { 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; }
/** * INTERNAL: * Compares the value with the value from the object (or cache). * Will return true if the object is newer. */ public boolean isNewerVersion(Object currentValue, Object domainObject, java.util.Vector primaryKey, AbstractSession session) { 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); } // bug 6342382: object's lock value is null, it is NOT newer than any newWriteLockFieldValue. if(writeLockFieldValue == null) { return false; } // 2.5.1.6 if the write lock value is null, then what ever we have is treated as newer. if (newWriteLockFieldValue == null) { return true; } if (!(newWriteLockFieldValue.after(writeLockFieldValue))) { return false; } return true; }
/** * PUBLIC: * Create a new TimestampLockingPolicy. * Defaults to using the time retrieved from the server. */ public TimestampLockingPolicy() { super(); this.useServerTime(); }