Logo Search packages:      
Sourcecode: mysql-connector-java version File versions  Download package

synchronized int [] com::mysql::jdbc::ServerPreparedStatement::executeBatch (  )  throws SQLException [inline]

See also:
java.sql.Statement::executeBatch()

Reimplemented from com::mysql::jdbc::PreparedStatement.

Definition at line 622 of file ServerPreparedStatement.java.

References com::mysql::jdbc::StatementImpl::batchedArgs, checkClosed(), com::mysql::jdbc::PreparedStatement::clearBatch(), com::mysql::jdbc::StatementImpl::clearWarnings(), com::mysql::jdbc::StatementImpl::connection, com::mysql::jdbc::PreparedStatement::executeUpdate(), com::mysql::jdbc::ConnectionImpl::getMutex(), com::mysql::jdbc::ConnectionImpl::isReadOnly(), parameterBindings, and sendTypesToServer.

                                                                   {
            if (this.connection.isReadOnly()) {
                  throw SQLError.createSQLException(Messages
                              .getString("ServerPreparedStatement.2") //$NON-NLS-1$
                              + Messages.getString("ServerPreparedStatement.3"), //$NON-NLS-1$
                              SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
            }

            checkClosed();

            synchronized (this.connection.getMutex()) {
                  clearWarnings();

                  // Store this for later, we're going to 'swap' them out
                  // as we execute each batched statement...
                  BindValue[] oldBindValues = this.parameterBindings;

                  try {
                        int[] updateCounts = null;

                        if (this.batchedArgs != null) {
                              int nbrCommands = this.batchedArgs.size();
                              updateCounts = new int[nbrCommands];

                              if (this.retrieveGeneratedKeys) {
                                    this.batchedGeneratedKeys = new ArrayList(nbrCommands);
                              }

                              for (int i = 0; i < nbrCommands; i++) {
                                    updateCounts[i] = -3;
                              }

                              SQLException sqlEx = null;

                              int commandIndex = 0;

                              BindValue[] previousBindValuesForBatch = null;

                              for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) {
                                    Object arg = this.batchedArgs.get(commandIndex);

                                    if (arg instanceof String) {
                                          updateCounts[commandIndex] = executeUpdate((String) arg);
                                    } else {
                                          this.parameterBindings = ((BatchedBindValues) arg).batchedParameterValues;

                                          try {
                                                // We need to check types each time, as
                                                // the user might have bound different
                                                // types in each addBatch()

                                                if (previousBindValuesForBatch != null) {
                                                      for (int j = 0; j < this.parameterBindings.length; j++) {
                                                            if (this.parameterBindings[j].bufferType != previousBindValuesForBatch[j].bufferType) {
                                                                  this.sendTypesToServer = true;

                                                                  break;
                                                            }
                                                      }
                                                }

                                                try {
                                                      updateCounts[commandIndex] = executeUpdate(false, true);
                                                } finally {
                                                      previousBindValuesForBatch = this.parameterBindings;
                                                }

                                                if (this.retrieveGeneratedKeys) {
                                                      java.sql.ResultSet rs = null;

                                                      try {
                                                            // we don't want to use our version,
                                                            // because we've altered the behavior of
                                                            // ours to support batch updates
                                                            // (catch-22)
                                                            // Ideally, what we need here is
                                                            // super.super.getGeneratedKeys()
                                                            // but that construct doesn't exist in
                                                            // Java, so that's why there's
                                                            // this kludge.
                                                            rs = getGeneratedKeysInternal();

                                                            while (rs.next()) {
                                                                  this.batchedGeneratedKeys
                                                                              .add(new ByteArrayRow(new byte[][] { rs
                                                                                          .getBytes(1) }));
                                                            }
                                                      } finally {
                                                            if (rs != null) {
                                                                  rs.close();
                                                            }
                                                      }
                                                }
                                          } catch (SQLException ex) {
                                                updateCounts[commandIndex] = EXECUTE_FAILED;

                                                if (this.continueBatchOnError) {
                                                      sqlEx = ex;
                                                } else {
                                                      int[] newUpdateCounts = new int[commandIndex];
                                                      System.arraycopy(updateCounts, 0,
                                                                  newUpdateCounts, 0, commandIndex);

                                                      throw new java.sql.BatchUpdateException(ex
                                                                  .getMessage(), ex.getSQLState(), ex
                                                                  .getErrorCode(), newUpdateCounts);
                                                }
                                          }
                                    }
                              }

                              if (sqlEx != null) {
                                    throw new java.sql.BatchUpdateException(sqlEx
                                                .getMessage(), sqlEx.getSQLState(), sqlEx
                                                .getErrorCode(), updateCounts);
                              }
                        }

                        return (updateCounts != null) ? updateCounts : new int[0];
                  } finally {
                        this.parameterBindings = oldBindValues;
                        this.sendTypesToServer = true;

                        clearBatch();
                  }
            }
      }


Generated by  Doxygen 1.6.0   Back to index