/**
   * INTERNAL: Execute the query. Get the rows and build the object from the rows.
   *
   * @exception DatabaseException - an error has occurred on the database
   * @return java.lang.Object collection of objects resulting from execution of query.
   */
  protected Object executeObjectLevelReadQuery() throws DatabaseException {
    Object result = null;

    if (getContainerPolicy().overridesRead()) {
      return getContainerPolicy().execute();
    }

    if (this.descriptor.isDescriptorForInterface()) {
      Object returnValue =
          this.descriptor.getInterfacePolicy().selectAllObjectsUsingMultipleTableSubclassRead(this);
      setExecutionTime(System.currentTimeMillis());
      return returnValue;
    }

    List rows = getQueryMechanism().selectAllRows();
    setExecutionTime(System.currentTimeMillis());

    // If using 1-m joins, must set all rows.
    if (hasJoining() && this.joinedAttributeManager.isToManyJoin()) {
      this.joinedAttributeManager.setDataResults(rows, this.session);
    }

    if (this.session.isUnitOfWork()) {
      result =
          registerResultInUnitOfWork(
              rows, (UnitOfWorkImpl) this.session, this.translationRow, true); //
    } else {
      result = getContainerPolicy().containerInstance(rows.size());
      this.descriptor.getObjectBuilder().buildObjectsInto(this, rows, result);
    }

    if (this.shouldIncludeData) {
      ComplexQueryResult complexResult = new ComplexQueryResult();
      complexResult.setResult(result);
      complexResult.setData(rows);
      return complexResult;
    }

    // Add the other (already registered) results and return them.
    if (getDescriptor().hasTablePerClassPolicy()) {
      result =
          containerPolicy.concatenateContainers(
              result,
              getDescriptor()
                  .getTablePerClassPolicy()
                  .selectAllObjectsUsingMultipleTableSubclassRead(this));
    }

    return result;
  }
  public void setup() {
    ConcurrentTestRefreshWithOptimisticLocking.execute = true;
    try {
      getSession().getLog().write("WARNING, some tests may take 3 minutes or more");
      getSession().getLog().flush();
    } catch (java.io.IOException e) {
    }

    try {
      DatabaseLogin login = (DatabaseLogin) getSession().getLogin().clone();
      ConcurrentTestRefreshWithOptimisticLocking.server =
          new Server(login, numThreads, numThreads + 2);
      ConcurrentTestRefreshWithOptimisticLocking.server.serverSession.setSessionLog(
          getSession().getSessionLog());
      ConcurrentTestRefreshWithOptimisticLocking.server.login();
      ConcurrentTestRefreshWithOptimisticLocking.server.copyDescriptors(getSession());
      ClassDescriptor empDesc =
          this.server.serverSession.getClassDescriptor(DeadLockEmployee.class);
      this.oldVersionRefresh = empDesc.shouldOnlyRefreshCacheIfNewerVersion();
      empDesc.onlyRefreshCacheIfNewerVersion();

    } catch (ValidationException ex) {
      this.verify();
    }

    for (int count = 0; count < numThreads; ++count) {
      ConcurrentTestRefreshWithOptimisticLocking.threadList[count] =
          new Thread(new ConcurrentTestRefreshWithOptimisticLocking(false, count).runnable());
      ConcurrentTestRefreshWithOptimisticLocking.timeList[count] = System.currentTimeMillis();
    }
  }
 public void watchOtherThreads() {
   long startTime = System.currentTimeMillis();
   while (((System.currentTimeMillis() - startTime) < (runTime + 30000))
       && ConcurrentTestRefreshWithOptimisticLocking.execute) {
     for (int localIdex = 0; localIdex < numThreads; ++localIdex) {
       if ((System.currentTimeMillis()
               - ConcurrentTestRefreshWithOptimisticLocking.timeList[localIdex])
           > 30000) {
         // System.out.println(" Number: " + localIdex + " time: " + (System.currentTimeMillis() -
         // this.timeList[localIdex]));
         execute = false;
         break;
       }
     }
     try {
       Thread.sleep(30000);
     } catch (InterruptedException ex) {
       throw new TestProblemException("Test thread was interrupted.  Test failed to run properly");
     }
   }
 }
 public void executeUntilStopped() {
   Session session =
       ConcurrentTestRefreshWithOptimisticLocking.server.serverSession.acquireClientSession();
   DeadLockAddress address =
       (DeadLockAddress)
           session.readObject(
               org.eclipse.persistence.testing.tests.clientserver.DeadLockAddress.class);
   DeadLockEmployee employee = (DeadLockEmployee) session.readObject(DeadLockEmployee.class);
   ReadObjectQuery query;
   if ((this.index % 2) != 0) {
     query = new ReadObjectQuery(address);
     query.refreshIdentityMapResult();
     query.setCascadePolicy(DatabaseQuery.CascadeAllParts);
   } else {
     query = new ReadObjectQuery(employee);
     query.refreshIdentityMapResult();
     query.setCascadePolicy(DatabaseQuery.CascadeAllParts);
   }
   while (ConcurrentTestRefreshWithOptimisticLocking.execute) {
     ConcurrentTestRefreshWithOptimisticLocking.timeList[this.index] = System.currentTimeMillis();
     session.executeQuery(query);
   }
   // System.out.println("BeingShutDown");
 }
  /**
   * INTERNAL: Check if existence can be determined without going to the database. Note that custom
   * query check is not require for does exist as the custom is always used. Used by unit of work,
   * and will return null if checkDatabaseIfInvalid is set and the cachekey is invalidated
   */
  public Object checkEarlyReturn(
      Object object, Vector primaryKey, AbstractSession session, AbstractRecord translationRow) {
    // For bug 3136413/2610803 building the selection criteria from an EJBQL string or
    // an example object is done just in time.
    buildSelectionCriteria(session);
    // Return false on null since it can't exist.  Little more done in case PK not set in the query
    if (object == null) {
      return Boolean.FALSE;
    }
    ClassDescriptor descriptor = session.getDescriptor(object.getClass());
    if (primaryKey == null) {
      primaryKey = getPrimaryKey();
      if (primaryKey == null) {
        primaryKey =
            descriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, session, true);
      }
    }
    if ((primaryKey == null) || (primaryKey.contains(null))) {
      return Boolean.FALSE;
    }

    // Need to do the cache check first if flag set or if we should check the cache only for
    // existence.
    if ((shouldCheckCacheForDoesExist() || this.checkCacheFirst)
        && !descriptor.isDescriptorForInterface()) {

      // If this is a UOW and modification queries have been executed, the cache cannot be trusted.
      if (this.checkDatabaseIfInvalid
          && (session.isUnitOfWork() && ((UnitOfWorkImpl) session).shouldReadFromDB())) {
        return null;
      }

      CacheKey cacheKey;
      Class objectClass = object.getClass();
      AbstractSession tempSession = session;
      while (tempSession.isUnitOfWork()) { // could be nested lets check all UOWs
        cacheKey =
            tempSession
                .getIdentityMapAccessorInstance()
                .getCacheKeyForObjectForLock(primaryKey, objectClass, descriptor);
        if (cacheKey != null) {
          // If in the UOW cache it can't be invalid.
          return Boolean.TRUE;
        }
        tempSession = ((UnitOfWorkImpl) tempSession).getParent();
      }
      // Did not find it registered in UOW so check main cache and check for invalidation.
      cacheKey =
          tempSession
              .getIdentityMapAccessorInstance()
              .getCacheKeyForObject(primaryKey, objectClass, descriptor);

      if ((cacheKey != null)) {
        // Assume that if there is a cachekey, object exists.
        if (this.checkDatabaseIfInvalid) {
          checkDescriptor(object, session);
          if (this.descriptor
              .getCacheInvalidationPolicy()
              .isInvalidated(cacheKey, System.currentTimeMillis())) {
            return null;
          }
        }

        Object objectFromCache = cacheKey.getObject();
        if ((session.isUnitOfWork()) && ((UnitOfWorkImpl) session).wasDeleted(objectFromCache)) {
          if (shouldCheckCacheForDoesExist()) {
            return Boolean.FALSE;
          }
        } else {
          return Boolean.TRUE;
        }
      } else if (shouldCheckCacheForDoesExist()) {
        // We know its not in cache, and a checkcache policy so return false.
        return Boolean.FALSE;
      }
    }
    // Check if we have to assume that the object does not exist.
    if (shouldAssumeNonExistenceForDoesExist()) {
      return Boolean.FALSE;
    }

    // Check to see if we only need to check that the object contains a primary key.
    if (shouldAssumeExistenceForDoesExist()) {
      return Boolean.TRUE;
    }

    return null;
  }