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

com.mysql.jdbc.ResultSetInternalMethods com::mysql::jdbc::ServerPreparedStatement::serverExecute ( int  maxRowsToRetrieve,
boolean  createStreamingResultSet,
Field[]  metadataFromCache 
) throws SQLException [inline, private]

Tells the server to execute this prepared statement with the current parameter bindings.

  • Server gets the command 'COM_EXECUTE' to execute the previously prepared query. If there is any param markers; then client will send the data in the following format:

  [COM_EXECUTE:1]
  [STMT_ID:4]
  [NULL_BITS:(param_count+7)/8)]
  [TYPES_SUPPLIED_BY_CLIENT(0/1):1]
  [[length]data]
  [[length]data] .. [[length]data].

  (Note: Except for string/binary types; all other types will not be
  supplied with length field)

 

Parameters:
maxRowsToRetrieve DOCUMENT ME!
createStreamingResultSet DOCUMENT ME!
Returns:
DOCUMENT ME!
Exceptions:
SQLException 

Definition at line 1089 of file ServerPreparedStatement.java.

References com::mysql::jdbc::StatementImpl::cancelTimeoutMutex, com::mysql::jdbc::StatementImpl::CancelTask::caughtWhileCancelling, com::mysql::jdbc::Buffer::clear(), com::mysql::jdbc::StatementImpl::connection, com::mysql::jdbc::profiler::ProfileEventSink::consumeEvent(), com::mysql::jdbc::StatementImpl::createStreamingResultSet(), com::mysql::jdbc::StatementImpl::currentCatalog, detectedLongParameterSwitch, com::mysql::jdbc::StatementImpl::eventSink, com::mysql::jdbc::MysqlIO::explainSlowQuery(), fieldCount, com::mysql::jdbc::ConnectionPropertiesImpl::getAutoGenerateTestcaseScript(), com::mysql::jdbc::ConnectionImpl::getCancelTimer(), com::mysql::jdbc::MysqlIO::getCurrentTimeNanosOrMillis(), com::mysql::jdbc::ConnectionPropertiesImpl::getEnableQueryTimeouts(), com::mysql::jdbc::ConnectionPropertiesImpl::getExplainSlowQueries(), com::mysql::jdbc::StatementImpl::getFetchSize(), com::mysql::jdbc::ConnectionPropertiesImpl::getGatherPerformanceMetrics(), com::mysql::jdbc::StatementImpl::getId(), com::mysql::jdbc::profiler::ProfileEventSink::getInstance(), com::mysql::jdbc::ConnectionImpl::getIO(), com::mysql::jdbc::ConnectionPropertiesImpl::getLogSlowQueries(), com::mysql::jdbc::ConnectionImpl::getMutex(), com::mysql::jdbc::Buffer::getPosition(), com::mysql::jdbc::MysqlIO::getQueryTimingUnits(), com::mysql::jdbc::StatementImpl::getResultSetConcurrency(), com::mysql::jdbc::StatementImpl::getResultSetType(), com::mysql::jdbc::MysqlIO::getSharedSendPacket(), com::mysql::jdbc::MysqlIO::getSlowQueryThreshold(), com::mysql::jdbc::ConnectionPropertiesImpl::getSlowQueryThresholdMillis(), com::mysql::jdbc::MysqlIO::hadWarnings, com::mysql::jdbc::ConnectionImpl::incrementNumberOfPreparedExecutes(), com::mysql::jdbc::ConnectionImpl::isAbonormallyLongQuery(), com::mysql::jdbc::ConnectionImpl::isCursorFetchEnabled(), com::mysql::jdbc::PreparedStatement::originalSql, parameterBindings, com::mysql::jdbc::StatementImpl::profileSQL, com::mysql::jdbc::MysqlIO::readAllResults(), com::mysql::jdbc::ConnectionImpl::registerQueryExecutionTime(), com::mysql::jdbc::ConnectionImpl::reportQueryTime(), resultFields, com::mysql::jdbc::StatementImpl::results, com::mysql::jdbc::StatementImpl::resultSetConcurrency, com::mysql::jdbc::StatementImpl::resultSetType, com::mysql::jdbc::MysqlIO::scanForAndThrowDataTruncation(), com::mysql::jdbc::MysqlIO::sendCommand(), sendTypesToServer, serverLongData(), serverStatementId, com::mysql::jdbc::Buffer::setPosition(), storeBinding(), com::mysql::jdbc::StatementImpl::timeoutInMillis, com::mysql::jdbc::ConnectionImpl::versionMeetsMinimum(), com::mysql::jdbc::Buffer::writeByte(), com::mysql::jdbc::Buffer::writeBytesNoNull(), com::mysql::jdbc::Buffer::writeInt(), and com::mysql::jdbc::Buffer::writeLong().

Referenced by executeInternal().

                                                                 {
            synchronized (this.connection.getMutex()) {
                  if (this.detectedLongParameterSwitch) {
                        // Check when values were bound
                        boolean firstFound = false;
                        long boundTimeToCheck = 0;
                        
                        for (int i = 0; i < this.parameterCount - 1; i++) {
                              if (this.parameterBindings[i].isLongData) {
                                    if (firstFound && boundTimeToCheck != 
                                          this.parameterBindings[i].boundBeforeExecutionNum) {                          
                                          throw SQLError.createSQLException(Messages
                                                      .getString("ServerPreparedStatement.11") //$NON-NLS-1$
                                                      + Messages.getString("ServerPreparedStatement.12"), //$NON-NLS-1$
                                                      SQLError.SQL_STATE_DRIVER_NOT_CAPABLE);
                                    } else {
                                          firstFound = true;
                                          boundTimeToCheck = this.parameterBindings[i].boundBeforeExecutionNum;
                                    }
                              }
                        }
                        
                        // Okay, we've got all "newly"-bound streams, so reset 
                        // server-side state to clear out previous bindings
                        
                        serverResetStatement();
                  }


                  // Check bindings
                  for (int i = 0; i < this.parameterCount; i++) {
                        if (!this.parameterBindings[i].isSet) {
                              throw SQLError.createSQLException(Messages
                                          .getString("ServerPreparedStatement.13") + (i + 1) //$NON-NLS-1$
                                          + Messages.getString("ServerPreparedStatement.14"),
                                          SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
                        }
                  }

                  //
                  // Send all long data
                  //
                  for (int i = 0; i < this.parameterCount; i++) {
                        if (this.parameterBindings[i].isLongData) {
                              serverLongData(i, this.parameterBindings[i]);
                        }
                  }

                  if (this.connection.getAutoGenerateTestcaseScript()) {
                        dumpExecuteForTestcase();
                  }

                  //
                  // store the parameter values
                  //
                  MysqlIO mysql = this.connection.getIO();

                  Buffer packet = mysql.getSharedSendPacket();

                  packet.clear();
                  packet.writeByte((byte) MysqlDefs.COM_EXECUTE);
                  packet.writeLong(this.serverStatementId);

                  boolean usingCursor = false;

                  if (this.connection.versionMeetsMinimum(4, 1, 2)) {
                        // we only create cursor-backed result sets if
                        // a) The query is a SELECT
                        // b) The server supports it
                        // c) We know it is forward-only (note this doesn't
                        // preclude updatable result sets)
                        // d) The user has set a fetch size
                        if (this.resultFields != null &&
                                    this.connection.isCursorFetchEnabled()
                                    && getResultSetType() == ResultSet.TYPE_FORWARD_ONLY
                                    && getResultSetConcurrency() == ResultSet.CONCUR_READ_ONLY
                                    && getFetchSize() > 0) {
                              packet.writeByte(MysqlDefs.OPEN_CURSOR_FLAG);
                              usingCursor = true;
                        } else {
                              packet.writeByte((byte) 0); // placeholder for flags
                        }

                        packet.writeLong(1); // placeholder for parameter
                                             // iterations
                  }

                  /* Reserve place for null-marker bytes */
                  int nullCount = (this.parameterCount + 7) / 8;

                  // if (mysql.versionMeetsMinimum(4, 1, 2)) {
                  // nullCount = (this.parameterCount + 9) / 8;
                  // }
                  int nullBitsPosition = packet.getPosition();

                  for (int i = 0; i < nullCount; i++) {
                        packet.writeByte((byte) 0);
                  }

                  byte[] nullBitsBuffer = new byte[nullCount];

                  /* In case if buffers (type) altered, indicate to server */
                  packet.writeByte(this.sendTypesToServer ? (byte) 1 : (byte) 0);

                  if (this.sendTypesToServer) {
                        /*
                         * Store types of parameters in first in first package that is
                         * sent to the server.
                         */
                        for (int i = 0; i < this.parameterCount; i++) {
                              packet.writeInt(this.parameterBindings[i].bufferType);
                        }
                  }

                  //
                  // store the parameter values
                  //
                  for (int i = 0; i < this.parameterCount; i++) {
                        if (!this.parameterBindings[i].isLongData) {
                              if (!this.parameterBindings[i].isNull) {
                                    storeBinding(packet, this.parameterBindings[i], mysql);
                              } else {
                                    nullBitsBuffer[i / 8] |= (1 << (i & 7));
                              }
                        }
                  }

                  //
                  // Go back and write the NULL flags
                  // to the beginning of the packet
                  //
                  int endPosition = packet.getPosition();
                  packet.setPosition(nullBitsPosition);
                  packet.writeBytesNoNull(nullBitsBuffer);
                  packet.setPosition(endPosition);

                  long begin = 0;

                  boolean logSlowQueries = this.connection.getLogSlowQueries();
                  boolean gatherPerformanceMetrics = this.connection
                              .getGatherPerformanceMetrics();

                  if (this.profileSQL || logSlowQueries || gatherPerformanceMetrics) {
                        begin = mysql.getCurrentTimeNanosOrMillis();
                  }

                  resetCancelledState();
                  
                  CancelTask timeoutTask = null;

                  try {
                        if (this.connection.getEnableQueryTimeouts() &&
                                    this.timeoutInMillis != 0
                                    && this.connection.versionMeetsMinimum(5, 0, 0)) {
                              timeoutTask = new CancelTask(this);
                              this.connection.getCancelTimer().schedule(timeoutTask, 
                                          this.timeoutInMillis);
                        }
                        
                        Buffer resultPacket = mysql.sendCommand(MysqlDefs.COM_EXECUTE,
                              null, packet, false, null);
                        
                        long queryEndTime = 0L;
                        
                        if (logSlowQueries || gatherPerformanceMetrics || this.profileSQL) {
                              queryEndTime = mysql.getCurrentTimeNanosOrMillis();
                        }
                        
                        if (timeoutTask != null) {
                              timeoutTask.cancel();
                              
                              if (timeoutTask.caughtWhileCancelling != null) {
                                    throw timeoutTask.caughtWhileCancelling;
                              }
                              
                              timeoutTask = null;
                        }
                        
                        synchronized (this.cancelTimeoutMutex) {
                              if (this.wasCancelled) {
                                    SQLException cause = null;
                                    
                                    if (this.wasCancelledByTimeout) {
                                          cause = new MySQLTimeoutException();
                                    } else {
                                          cause = new MySQLStatementCancelledException();
                                    }
                                    
                                    resetCancelledState();
                                    
                                    throw cause;
                              }
                        }

                        boolean queryWasSlow = false;
                        
                        if (logSlowQueries || gatherPerformanceMetrics) {
                              long elapsedTime = queryEndTime - begin;

                              if (logSlowQueries) {
                              if (this.useAutoSlowLog) {
                                    queryWasSlow = elapsedTime > this.connection.getSlowQueryThresholdMillis();
                              } else {
                                    queryWasSlow = this.connection.isAbonormallyLongQuery(elapsedTime);
                                    
                                    this.connection.reportQueryTime(elapsedTime);
                              }
                              }

                              if (queryWasSlow) {
                                    
                                    StringBuffer mesgBuf = new StringBuffer(
                                                48 + this.originalSql.length());
                                    mesgBuf.append(Messages
                                                .getString("ServerPreparedStatement.15")); //$NON-NLS-1$
                                    mesgBuf.append(mysql.getSlowQueryThreshold());
                                    mesgBuf.append(Messages
                                                .getString("ServerPreparedStatement.15a")); //$NON-NLS-1$
                                    mesgBuf.append(elapsedTime);
                                    mesgBuf.append(Messages
                                                .getString("ServerPreparedStatement.16")); //$NON-NLS-1$

                                    mesgBuf.append("as prepared: ");
                                    mesgBuf.append(this.originalSql);
                                    mesgBuf.append("\n\n with parameters bound:\n\n");
                                    mesgBuf.append(asSql(true));

                                    this.eventSink
                                                .consumeEvent(new ProfilerEvent(
                                                            ProfilerEvent.TYPE_SLOW_QUERY,
                                                            "", this.currentCatalog, this.connection.getId(), //$NON-NLS-1$
                                                            getId(), 0, System.currentTimeMillis(),
                                                            elapsedTime, mysql
                                                                        .getQueryTimingUnits(), null,
                                                            new Throwable(), mesgBuf.toString()));
                              }

                              if (gatherPerformanceMetrics) {
                                    this.connection.registerQueryExecutionTime(elapsedTime);
                              }
                        }

                        this.connection.incrementNumberOfPreparedExecutes();

                        if (this.profileSQL) {
                              this.eventSink = ProfileEventSink
                                          .getInstance(this.connection);

                              this.eventSink.consumeEvent(new ProfilerEvent(
                                          ProfilerEvent.TYPE_EXECUTE,
                                          "", this.currentCatalog, //$NON-NLS-1$
                                          this.connectionId, this.statementId, -1, System
                                                      .currentTimeMillis(), (int) (mysql
                                                      .getCurrentTimeNanosOrMillis() - begin),
                                          mysql.getQueryTimingUnits(), null, new Throwable(),
                                          truncateQueryToLog(asSql(true))));
                        }
      
                        com.mysql.jdbc.ResultSetInternalMethods rs = mysql.readAllResults(this,
                                    maxRowsToRetrieve, this.resultSetType,
                                    this.resultSetConcurrency, createStreamingResultSet,
                                    this.currentCatalog, resultPacket, true, this.fieldCount,
                                    metadataFromCache);
                        
                        if (this.profileSQL) {
                              long fetchEndTime = mysql.getCurrentTimeNanosOrMillis();

                              this.eventSink.consumeEvent(new ProfilerEvent(
                                          ProfilerEvent.TYPE_FETCH,
                                          "", this.currentCatalog, this.connection.getId(), //$NON-NLS-1$
                                          getId(), 0 /* FIXME rs.resultId */, System.currentTimeMillis(),
                                          (fetchEndTime - queryEndTime), mysql
                                                      .getQueryTimingUnits(), null,
                                          new Throwable(), null));
                        }
      
                        if (queryWasSlow && this.connection.getExplainSlowQueries()) {
                              String queryAsString = asSql(true);

                              mysql.explainSlowQuery(queryAsString.getBytes(),
                                          queryAsString);
                        }
                        
                        if (!createStreamingResultSet && 
                                    this.serverNeedsResetBeforeEachExecution) {
                              serverResetStatement(); // clear any long data...
                        }
      
                        
                        this.sendTypesToServer = false;
                        this.results = rs;
      
                        if (mysql.hadWarnings()) {
                        mysql.scanForAndThrowDataTruncation();
                    }
                        
                        return rs;
                  } finally {
                        if (timeoutTask != null) {
                              timeoutTask.cancel();
                        }
                  }
            }
      }


Generated by  Doxygen 1.6.0   Back to index