1 Reply Latest reply on Dec 12, 2003 2:07 PM by juha

    Optimistic locking and timestamp

    radl01

      Hi all,

      I had a simple problem with optimistic locking and timestamp column.

      Situation:

      1) SSB method getXXX loads data from EB to struct and than to client
      2) client change data except timestamp
      3) client call SSB method setXXX to store data to EB (database)
      3a) EB home call findByPK to find specific bean
      3b) setter methods are called to store data from struct to EB

      In point 3 is the problem because when I change timestamp in DB between point 1 and 3 I will expect that Exception will be rised bacause the timestamp was changed. But I receive no Exception because findByPK load new value to locking field.

      I think that container should use timestamp from bean (field.getValue(ctx);) instead from locking cach (field.getLockedValue(ctx);). in where clausule in case of timestamp or version_co strategy.

      I've made a small change in execute method in JDBCStoreEntityCommand class :



      public void execute(EntityEnterpriseContext ctx)
      {
      // scheduled for batch cascade-delete instance should not be updated
      // because foreign key fields could be updated to null and cascade-delete will fail.
      if(!entity.isDirty(ctx) || entity.isScheduledForBatchCascadeDelete(ctx))

      .....
      ....
      // WHERE: set optimistically locked field values
      if(hasLockedFields)
      {
      lockedIterator.reset();
      while(lockedIterator.hasNext())
      {
      JDBCCMPFieldBridge field = lockedIterator.next();

      // ***************************************************
      // add by Jan Radl, FSS, spol. s r.o. 12/12/2003
      JDBCEntityMetaData metadata = entity.getMetaData();
      JDBCOptimisticLockingMetaData lockMetaData = metadata.getOptimisticLocking();
      Integer strategy = lockMetaData.getLockingStrategy();
      Object value;
      if ( (strategy == JDBCOptimisticLockingMetaData.VERSION_COLUMN_STRATEGY) ||
      (strategy == JDBCOptimisticLockingMetaData.TIMESTAMP_COLUMN_STRATEGY) ) {
      String fname = field.getFieldName();
      if(log.isTraceEnabled())
      {
      log.trace(" lockingFieldName=" + fname + " index=" + index);
      }
      dirtyIterator.reset();
      JDBCCMPFieldBridge fieldx = null;
      while(dirtyIterator.hasNext()) {
      fieldx = dirtyIterator.next();
      if (fieldx.getFieldName().equalsIgnoreCase(fname)) {
      break;
      }
      }
      if (fieldx != null) {
      field = fieldx;
      value = field.getValue(ctx);
      } else {
      value = field.getLockedValue(ctx);
      }
      } else {
      value = field.getLockedValue(ctx);
      }
      index = field.setArgumentParameters(ps, index, value);
      // **************************************************
      /*
      Object value = field.getLockedValue(ctx);
      index = field.setArgumentParameters(ps, index, value);
      */
      }
      }

      // execute statement
      rowsAffected = ps.executeUpdate();


      Jan