Exemple #1
0
  /** 执行批量操作 */
  public int executeBatchUpdate(Connection con) throws SQLException {
    int totalUpdatedRows = 0;
    int currentBatchCount = 0;
    String preSql = "", curSql = "";
    PreparedStatement updateStatement = null;
    int[] batchRows = null;

    try {
      Iterator itor = batchRequestList.iterator();
      while (itor.hasNext()) {
        SqlRequest curReqest = (SqlRequest) itor.next();
        curSql = curReqest.getSqlText();
        String sqlId = curReqest.getSqlId();
        String[] paramNames = curReqest.getParamNames();
        Object[] paramValues = curReqest.getParamValues();
        int[] paramSqlTypeCodes = curReqest.getParamSqlTypeCodes();
        JdaTypePersister[] paramTypePersisters = curReqest.getParamTypePersisters();
        ParamValueMode[] paramValueModes = curReqest.getParamValueModes();
        JdaSessionImpl session = curReqest.getRequestSession();

        /**
         * 满足批量更新的条件如下: 1:上一个Statement更新进去的数据已经达到Size,则需要执行前面Statement,
         * 2:当前的SQL与前面执行SQL不同时候,则需要执行前面Statement, 3:到达数据列表的终点,不论1与2,都需要执行当前的Statement
         */
        if (updateStatement == null)
          updateStatement = SqlRequestHandler.createGeneralStatement(con, curSql);

        if (currentBatchCount == batchUpdateSize) { // Size已满,需要执行批量更新了
          batchRows = updateStatement.executeBatch();
          totalUpdatedRows += getUpdateCount(batchRows);
          updateStatement.clearBatch();
          currentBatchCount = 0;
        }

        if (curSql.equalsIgnoreCase(preSql)) {
          ParamFactory.setParamValues(
              session,
              sqlId,
              updateStatement,
              paramNames,
              paramValues,
              paramSqlTypeCodes,
              paramTypePersisters,
              paramValueModes);
          updateStatement.addBatch();
          currentBatchCount++;
          preSql = curSql;
        } else if (!curSql.equalsIgnoreCase(preSql)) {
          batchRows = updateStatement.executeBatch();
          totalUpdatedRows += getUpdateCount(batchRows);
          updateStatement.clearBatch();
          currentBatchCount = 0;
          CloseUtil.close(updateStatement);
          updateStatement = null;

          updateStatement = SqlRequestHandler.createGeneralStatement(con, curSql);
          ParamFactory.setParamValues(
              session,
              sqlId,
              updateStatement,
              paramNames,
              paramValues,
              paramSqlTypeCodes,
              paramTypePersisters,
              paramValueModes);
          currentBatchCount++;
          preSql = curSql;
        }
      }

      if (currentBatchCount > 0) {
        batchRows = updateStatement.executeBatch();
        totalUpdatedRows += getUpdateCount(batchRows);
        updateStatement.clearBatch();
        currentBatchCount = 0;
        CloseUtil.close(updateStatement);
        updateStatement = null;
      }
    } catch (SQLException e) {
      if (updateStatement != null) {
        try {
          updateStatement.clearBatch();
          CloseUtil.close(updateStatement);
          updateStatement = null;
        } catch (Exception ee) {
        }
      }
      throw e;
    }

    return totalUpdatedRows;
  }
Exemple #2
0
  /** 正常情况下,一条一条执行 */
  private int executeGeneralUpdate(Connection con) throws SQLException {
    int updatedRows, totalUpdatedRows = 0;
    String preSqlText = "", curSqlText = "";
    PreparedStatement statement = null;

    try {
      Iterator itor = batchRequestList.iterator();
      while (itor.hasNext()) {
        SqlRequest curReqest = (SqlRequest) itor.next();
        String sqlId = curReqest.getSqlId();
        curSqlText = curReqest.getSqlText();
        Object paramObject = curReqest.getParamObject();
        String[] paramNames = curReqest.getParamNames();
        Object[] paramValues = curReqest.getParamValues();
        int[] paramSqlTypeCodes = curReqest.getParamSqlTypeCodes();
        JdaTypePersister[] paramTypePersisters = curReqest.getParamTypePersisters();
        ParamValueMode[] paramValueModes = curReqest.getParamValueModes();
        JdaSessionImpl session = curReqest.getRequestSession();

        if (statement == null)
          statement = SqlRequestHandler.createGeneralStatement(con, curSqlText);

        if (!curSqlText.equals(preSqlText)) {
          CloseUtil.close(statement);
          statement = null;
          statement = SqlRequestHandler.createGeneralStatement(con, curSqlText);

          ParamFactory.setParamValues(
              session,
              sqlId,
              statement,
              paramNames,
              paramValues,
              paramSqlTypeCodes,
              paramTypePersisters,
              paramValueModes);
          updatedRows = statement.executeUpdate();
          preSqlText = curSqlText;

          totalUpdatedRows = totalUpdatedRows + updatedRows;
        } else {
          ParamFactory.setParamValues(
              session,
              sqlId,
              statement,
              paramNames,
              paramValues,
              paramSqlTypeCodes,
              paramTypePersisters,
              paramValueModes);
          updatedRows = statement.executeUpdate();

          if (statement instanceof CallableStatement
              && curReqest.getParamObject() != null
              && !curReqest
                  .getRequestSession()
                  .supportsPersisterType(
                      curReqest.getParamObject().getClass())) // 当前为存储过程调用,则需要读取那些out类型的参数结果
          ParamFactory.readCallStatement(
                session,
                sqlId,
                (CallableStatement) statement,
                paramObject,
                paramNames,
                paramTypePersisters,
                paramValueModes);
          preSqlText = curSqlText;
          totalUpdatedRows = totalUpdatedRows + updatedRows;
        }
      }
      return totalUpdatedRows;
    } finally {
      CloseUtil.close(statement);
    }
  }