Ejemplo n.º 1
0
  /**
   * Modifies the DO within its table. Performs recursive commit/delete on referenced DOs; all
   * operations occur within a single transaction to allow rollback in the event of error. Only the
   * creator of the transaction releases it.
   *
   * @param dbt The transaction object to use for this operation.
   * @param delete True if doing a delete, otherwise doing insert/update.
   * @exception com.lutris.appserver.server.sql.DatabaseManagerException if a Transaction can not be
   *     created.
   * @exception RefAssertionException thrown by okTo method.
   * @exception java.sql.SQLException if any SQL errors occur.
   */
  protected void modifyDO(DBTransaction dbt, boolean delete)
      throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException,
          DBRowUpdateException, QueryException {
    boolean ownTransaction = false;
    try {
      if (null == dbt) {
        DatabaseManager dbm = Enhydra.getDatabaseManager();
        dbt = dbm.createTransaction(); // create a transaction
        ownTransaction = true;
      }
      if (null == dbt)
        throw new DatabaseManagerException("DatabaseManager.createTransaction returned null.");
      if (delete) {
        // Code to perform cascading deletes is generated here
        // if cascading deletes are not supported by the database.

        {
          // perform cascading delete on referring table
          jobmatch.data.CompanyBDO[] a = getCompanyBDOArray();
          for (int i = 0; i < a.length; i++) {
            a[i].delete(dbt);
          }
        }

        // The following line keeps the compiler happy
        // when the CASCADING_DELETES tag is empty.
        if (false) throw new QueryException("XXX");
      } else {
        // commit referenced DOs.

      }
      if (false) {
        // This throw is here to keep the compiler happy
        // in the case of a DO that does not refer to other DOs.
        // In that case, the above delete/commit code blocks will be empty
        // and throw nothing.
        throw new DataObjectException("foo");
      }
      if (delete) {
        dbt.delete(DO);
      } else {
        if (DO.isLoaded()) dbt.insert(DO); // dbt.insert() handles insertions and updates
      }
      if (ownTransaction) {
        dbt.commit(); // commit the transaction
      }
    } catch (DataObjectException doe) {
      throw doe;
    } catch (SQLException sqle) {
      StringBuffer message = new StringBuffer("Failed to insert/update DO: ");
      message.append(sqle.getMessage());

      // rollback, if necessary
      if (ownTransaction) {
        try {
          dbt.rollback();
        } catch (SQLException sqle2) {
          message.insert(0, "\n");
          message.insert(0, sqle2.getMessage());
          message.insert(0, "Rollback failed: ");
        }
      }
      throw new SQLException(message.toString());
    } finally {
      // release the transaction, if any
      if (ownTransaction) {
        dbt.release();
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Modifies the DO within its table. Performs recursive commit/delete on referenced DOs; all
   * operations occur within a single transaction to allow rollback in the event of error. Only the
   * creator of the transaction releases it.
   *
   * @param dbt The transaction object to use for this operation.
   * @param delete True if doing a delete, otherwise doing insert/update.
   * @exception com.lutris.appserver.server.sql.DatabaseManagerException if a Transaction can not be
   *     created.
   * @exception RefAssertionException thrown by okTo method.
   * @exception java.sql.SQLException if any SQL errors occur.
   */
  protected void modifyDO(DBTransaction dbt, boolean delete)
      throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException,
          DBRowUpdateException, QueryException {
    boolean ownTransaction = false;
    try {
      if (null == dbt) {
        DatabaseManager dbm = Enhydra.getDatabaseManager();
        dbt = dbm.createTransaction(); // create a transaction
        ownTransaction = true;
      }
      if (null == dbt)
        throw new DatabaseManagerException("DatabaseManager.createTransaction returned null.");
      if (delete) {
        // Code to perform cascading deletes is generated here
        // if cascading deletes are not supported by the database.

        // The following line keeps the compiler happy
        // when the CASCADING_DELETES tag is empty.
        if (false) throw new QueryException("XXX");
      } else {
        // commit referenced DOs.
        jobmatch.data.CandidateDO Candidate_DO = DO.getCandidate();
        if (null != Candidate_DO) {
          if (Candidate_DO.isLoaded()) {
            okToCommitCandidate(Candidate_DO);
            jobmatch.data.CandidateBDO b = jobmatch.data.CandidateBDO.createExisting(Candidate_DO);
            b.commit(dbt);
          } else {
            // since the referenced DO is not loaded,
            // it cannot be dirty, so there is no need to commit it.
          }
        } else {
          if (!false)
            throw new RefAssertionException(
                "Cannot commit EmployerCandidateBDO ( "
                    + toString()
                    + " ) because Candidate is not allowed to be null.");
        }
        jobmatch.data.EmployerDO Employer_DO = DO.getEmployer();
        if (null != Employer_DO) {
          if (Employer_DO.isLoaded()) {
            okToCommitEmployer(Employer_DO);
            jobmatch.data.EmployerBDO b = jobmatch.data.EmployerBDO.createExisting(Employer_DO);
            b.commit(dbt);
          } else {
            // since the referenced DO is not loaded,
            // it cannot be dirty, so there is no need to commit it.
          }
        } else {
          if (!false)
            throw new RefAssertionException(
                "Cannot commit EmployerCandidateBDO ( "
                    + toString()
                    + " ) because Employer is not allowed to be null.");
        }
      }
      if (false) {
        // This throw is here to keep the compiler happy
        // in the case of a DO that does not refer to other DOs.
        // In that case, the above delete/commit code blocks will be empty
        // and throw nothing.
        throw new DataObjectException("foo");
      }
      if (delete) {
        dbt.delete(DO);
      } else {
        if (DO.isLoaded()) dbt.insert(DO); // dbt.insert() handles insertions and updates
      }
      if (ownTransaction) {
        dbt.commit(); // commit the transaction
      }
    } catch (DataObjectException doe) {
      throw doe;
    } catch (SQLException sqle) {
      StringBuffer message = new StringBuffer("Failed to insert/update DO: ");
      message.append(sqle.getMessage());

      // rollback, if necessary
      if (ownTransaction) {
        try {
          dbt.rollback();
        } catch (SQLException sqle2) {
          message.insert(0, "\n");
          message.insert(0, sqle2.getMessage());
          message.insert(0, "Rollback failed: ");
        }
      }
      throw new SQLException(message.toString());
    } finally {
      // release the transaction, if any
      if (ownTransaction) {
        dbt.release();
      }
    }
  }
  /**
   * Perform the query on the database, and prepare the array of returned DO objects.
   *
   * @exception DataObjectException If a database access error occurs.
   * @exception NonUniqueQueryException If too many rows were found.
   */
  private void runQuery() throws DataObjectException, NonUniqueQueryException {
    needToRun = false;
    arrayIndex = -1;

    DBQuery dbQuery = null;
    try {
      Vector results;
      /*
         if ( cacheHits.size() > 0 ) {
      // The setQuery methods build up the cacheHits.
      // If the cache had our desired objects,
      // we don't query the database, so we have no ResultSet.
      results = cacheHits;	 // executeQuery saw cache hits
         } else {
      // If the cache doesn't exist, or could not handle the query,
      // then we actually query the database.
      dbQuery = Enhydra.getDatabaseManager().createQuery();
      dbQuery.query( this );    // invokes executeQuery
             results = new Vector();
             while ( resultSet.next() ) {
          results.addElement( new SoftwareCandidateDO ( resultSet ) );
             }
         }
         */
      if (/* partialCache && */ 0 == cacheHits.size()) hitDb = true;
      if (hitDb) {
        dbQuery = Enhydra.getDatabaseManager().createQuery();
        dbQuery.query(this); // invokes executeQuery
        results = new Vector();
        while (resultSet.next()) {
          //		    results.addElement( new SoftwareCandidateDO ( resultSet ) );
          results.addElement(SoftwareCandidateDO.createExisting(resultSet));
        }
      } else {
        results = cacheHits; // executeQuery saw cache hits
      }

      if (results.size() > 1 && uniqueInstance)
        throw new NonUniqueQueryException("Too many rows returned from database");
      DOs = new SoftwareCandidateDO[results.size()];
      for (int i = 0; i < results.size(); i++) {
        DOs[i] = (SoftwareCandidateDO) results.elementAt(i);
      }
      arrayIndex = 0;
    } catch (SQLException se) {
      if (null == se.getSQLState()) {
        throw new DataObjectException("Unknown SQLException", se);
      }
      if (se.getSQLState().startsWith("02") && se.getErrorCode() == 100) {
        throw new DataObjectException("Update or delete DO is out of synch", se);
      } else if (se.getSQLState().equals("S1000") && se.getErrorCode() == -268) {
        throw new DataObjectException("Integrity constraint violation", se);
      } else {
        throw new DataObjectException("Data Object Error", se);
      }
    } catch (ObjectIdException oe) {
      throw new DataObjectException("Object ID Error", oe);
    } catch (DatabaseManagerException de) {
      throw new DataObjectException("Database connection Error", de);
    } finally {
      if (null != dbQuery) dbQuery.release();
    }
  }