public String applyLocksToSql(String sql, Map aliasedLockModes, Map keyColumnNames) {
    Iterator itr = aliasedLockModes.entrySet().iterator();
    StringBuffer buffer = new StringBuffer(sql);
    int correction = 0;
    while (itr.hasNext()) {
      final Map.Entry entry = (Map.Entry) itr.next();
      final LockMode lockMode = (LockMode) entry.getValue();
      if (lockMode.greaterThan(LockMode.READ)) {
        final String alias = (String) entry.getKey();
        int start = -1, end = -1;
        if (sql.endsWith(" " + alias)) {
          start = (sql.length() - alias.length()) + correction;
          end = start + alias.length();
        } else {
          int position = sql.indexOf(" " + alias + " ");
          if (position <= -1) {
            position = sql.indexOf(" " + alias + ",");
          }
          if (position > -1) {
            start = position + correction + 1;
            end = start + alias.length();
          }
        }

        if (start > -1) {
          final String lockHint = appendLockHint(lockMode, alias);
          buffer.replace(start, end, lockHint);
          correction += (lockHint.length() - alias.length());
        }
      }
    }
    return buffer.toString();
  }
 private int determineHashCode() {
   int result = alias != null ? alias.hashCode() : 0;
   result = 31 * result + (getClass().getName().hashCode());
   result = 31 * result + (lockMode != null ? lockMode.hashCode() : 0);
   result = 31 * result + (propertyResults != null ? propertyResults.hashCode() : 0);
   return result;
 }
 public String appendLockHint(LockMode mode, String tableName) {
   if (mode.greaterThan(LockMode.READ)) {
     return tableName + " holdlock";
   } else {
     return tableName;
   }
 }
Example #4
0
 /**
  * Custom deserialization routine used during deserialization of a Session/PersistenceContext for
  * increased performance.
  *
  * @param ois The stream from which to read the entry.
  * @param persistenceContext The context being deserialized.
  * @return The deserialized EntityEntry
  * @throws IOException If a stream error occurs
  * @throws ClassNotFoundException If any of the classes declared in the stream cannot be found
  */
 public static EntityEntry deserialize(
     ObjectInputStream ois, PersistenceContext persistenceContext)
     throws IOException, ClassNotFoundException {
   String previousStatusString;
   return new EntityEntry(
       // this complexity comes from non-flushed changes, should really look at how that reattaches
       // entries
       (persistenceContext.getSession() == null
           ? null
           : persistenceContext.getSession().getFactory()),
       (String) ois.readObject(),
       (Serializable) ois.readObject(),
       EntityMode.parse((String) ois.readObject()),
       (String) ois.readObject(),
       Status.valueOf((String) ois.readObject()),
       ((previousStatusString = (String) ois.readObject()).length() == 0
           ? null
           : Status.valueOf(previousStatusString)),
       (Object[]) ois.readObject(),
       (Object[]) ois.readObject(),
       ois.readObject(),
       LockMode.valueOf((String) ois.readObject()),
       ois.readBoolean(),
       ois.readBoolean(),
       ois.readBoolean(),
       persistenceContext);
 }
 /**
  * Construct locking strategy.
  *
  * @param lockable The metadata for the entity to be locked.
  * @param lockMode Indicates the type of lock to be acquired.
  */
 public OptimisticForceIncrementLockingStrategy(Lockable lockable, LockMode lockMode) {
   this.lockable = lockable;
   this.lockMode = lockMode;
   if (lockMode.lessThan(LockMode.OPTIMISTIC_FORCE_INCREMENT)) {
     throw new HibernateException(
         "[" + lockMode + "] not valid for [" + lockable.getEntityName() + "]");
   }
 }
  @Override
  public String applyLocksToSql(String sql, LockOptions aliasedLockOptions, Map keyColumnNames) {
    // TODO:  merge additional lockoptions support in Dialect.applyLocksToSql
    final Iterator itr = aliasedLockOptions.getAliasLockIterator();
    final StringBuilder buffer = new StringBuilder(sql);
    int correction = 0;
    while (itr.hasNext()) {
      final Map.Entry entry = (Map.Entry) itr.next();
      final LockMode lockMode = (LockMode) entry.getValue();
      if (lockMode.greaterThan(LockMode.READ)) {
        final String alias = (String) entry.getKey();
        int start = -1;
        int end = -1;
        if (sql.endsWith(" " + alias)) {
          start = (sql.length() - alias.length()) + correction;
          end = start + alias.length();
        } else {
          int position = sql.indexOf(" " + alias + " ");
          if (position <= -1) {
            position = sql.indexOf(" " + alias + ",");
          }
          if (position > -1) {
            start = position + correction + 1;
            end = start + alias.length();
          }
        }

        if (start > -1) {
          final String lockHint = appendLockHint(lockMode, alias);
          buffer.replace(start, end, lockHint);
          correction += (lockHint.length() - alias.length());
        }
      }
    }
    return buffer.toString();
  }
Example #7
0
 /**
  * Custom serialization routine used during serialization of a Session/PersistenceContext for
  * increased performance.
  *
  * @param oos The stream to which we should write the serial data.
  * @throws IOException If a stream error occurs
  */
 public void serialize(ObjectOutputStream oos) throws IOException {
   oos.writeObject(entityName);
   oos.writeObject(id);
   oos.writeObject(entityMode.toString());
   oos.writeObject(tenantId);
   oos.writeObject(status.name());
   oos.writeObject((previousStatus == null ? "" : previousStatus.name()));
   // todo : potentially look at optimizing these two arrays
   oos.writeObject(loadedState);
   oos.writeObject(deletedState);
   oos.writeObject(version);
   oos.writeObject(lockMode.toString());
   oos.writeBoolean(existsInDatabase);
   oos.writeBoolean(isBeingReplicated);
   oos.writeBoolean(loadedWithLazyPropertiesUnfetched);
 }
 /**
  * Construct a locking strategy based on SQL UPDATE statements.
  *
  * @param lockable The metadata for the entity to be locked.
  * @param lockMode Indictates the type of lock to be acquired. Note that read-locks are not valid
  *     for this strategy.
  */
 public UpdateLockingStrategy(Lockable lockable, LockMode lockMode) {
   this.lockable = lockable;
   this.lockMode = lockMode;
   if (lockMode.lessThan(LockMode.UPGRADE)) {
     throw new HibernateException("[" + lockMode + "] not valid for update statement");
   }
   if (!lockable.isVersioned()) {
     log.warn(
         "write locks via update not supported for non-versioned entities ["
             + lockable.getEntityName()
             + "]");
     this.sql = null;
   } else {
     this.sql = generateLockString();
   }
 }
Example #9
0
 public LockingStrategy getLockingStrategy(Lockable lockable, LockMode lockMode) {
   // InterSystems Cache' does not current support "SELECT ... FOR UPDATE" syntax...
   // Set your transaction mode to READ_COMMITTED before using
   if (lockMode == LockMode.PESSIMISTIC_FORCE_INCREMENT) {
     return new PessimisticForceIncrementLockingStrategy(lockable, lockMode);
   } else if (lockMode == LockMode.PESSIMISTIC_WRITE) {
     return new PessimisticWriteUpdateLockingStrategy(lockable, lockMode);
   } else if (lockMode == LockMode.PESSIMISTIC_READ) {
     return new PessimisticReadUpdateLockingStrategy(lockable, lockMode);
   } else if (lockMode == LockMode.OPTIMISTIC) {
     return new OptimisticLockingStrategy(lockable, lockMode);
   } else if (lockMode == LockMode.OPTIMISTIC_FORCE_INCREMENT) {
     return new OptimisticForceIncrementLockingStrategy(lockable, lockMode);
   } else if (lockMode.greaterThan(LockMode.READ)) {
     return new UpdateLockingStrategy(lockable, lockMode);
   } else {
     return new SelectLockingStrategy(lockable, lockMode);
   }
 }
 /**
  * Custom deserialization routine used during deserialization of a Session/PersistenceContext for
  * increased performance.
  *
  * @param ois The stream from which to read the entry.
  * @param persistenceContext The context being deserialized.
  * @return The deserialized EntityEntry
  * @throws IOException If a stream error occurs
  * @throws ClassNotFoundException If any of the classes declared in the stream cannot be found
  */
 public static EntityEntry deserialize(
     ObjectInputStream ois, PersistenceContext persistenceContext)
     throws IOException, ClassNotFoundException {
   String previousStatusString;
   return new EntityEntry(
       persistenceContext.getSession().getFactory(),
       (String) ois.readObject(),
       (Serializable) ois.readObject(),
       EntityMode.parse((String) ois.readObject()),
       (String) ois.readObject(),
       Status.valueOf((String) ois.readObject()),
       (previousStatusString = (String) ois.readObject()).length() == 0
           ? null
           : Status.valueOf(previousStatusString),
       (Object[]) ois.readObject(),
       (Object[]) ois.readObject(),
       ois.readObject(),
       LockMode.valueOf((String) ois.readObject()),
       ois.readBoolean(),
       ois.readBoolean(),
       ois.readBoolean(),
       persistenceContext);
 }
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    NativeSQLQueryNonScalarReturn that = (NativeSQLQueryNonScalarReturn) o;

    if (alias != null ? !alias.equals(that.alias) : that.alias != null) {
      return false;
    }
    if (lockMode != null ? !lockMode.equals(that.lockMode) : that.lockMode != null) {
      return false;
    }
    if (propertyResults != null
        ? !propertyResults.equals(that.propertyResults)
        : that.propertyResults != null) {
      return false;
    }

    return true;
  }