in src/main/user-impl/java/com/mysql/cj/jdbc/StatementImpl.java [703:838]
private boolean executeInternal(String sql, boolean returnGeneratedKeys) throws SQLException {
JdbcConnection locallyScopedConn = checkClosed();
Lock connectionLock = locallyScopedConn.getConnectionLock();
connectionLock.lock();
try {
checkClosed();
TelemetrySpan span = getSession().getTelemetryHandler().startSpan(TelemetrySpanName.STMT_EXECUTE);
try (TelemetryScope scope = span.makeCurrent()) {
String dbOperation = QueryInfo.getStatementKeyword(sql, this.session.getServerSession().isNoBackslashEscapesSet());
span.setAttribute(TelemetryAttribute.DB_NAME, this::getCurrentDatabase);
span.setAttribute(TelemetryAttribute.DB_OPERATION, dbOperation);
span.setAttribute(TelemetryAttribute.DB_STATEMENT, dbOperation + TelemetryAttribute.STATEMENT_SUFFIX);
span.setAttribute(TelemetryAttribute.DB_SYSTEM, TelemetryAttribute.DB_SYSTEM_DEFAULT);
span.setAttribute(TelemetryAttribute.DB_USER, () -> this.connection.getUser());
span.setAttribute(TelemetryAttribute.THREAD_ID, () -> Thread.currentThread().getId());
span.setAttribute(TelemetryAttribute.THREAD_NAME, () -> Thread.currentThread().getName());
checkNullOrEmptyQuery(sql);
resetCancelledState();
implicitlyCloseAllOpenResults();
clearWarnings();
if (sql.charAt(0) == '/') {
if (sql.startsWith(PING_MARKER)) {
doPingInstead();
return true;
}
}
this.retrieveGeneratedKeys = returnGeneratedKeys;
this.lastQueryIsOnDupKeyUpdate = returnGeneratedKeys
&& QueryInfo.firstCharOfStatementUc(sql, this.session.getServerSession().isNoBackslashEscapesSet()) == 'I'
&& containsOnDuplicateKeyInString(sql);
if (!QueryInfo.isReadOnlySafeQuery(sql, this.session.getServerSession().isNoBackslashEscapesSet()) && locallyScopedConn.isReadOnly()) {
throw SQLError.createSQLException(Messages.getString("Statement.27") + Messages.getString("Statement.28"),
MysqlErrorNumbers.SQLSTATE_CONNJ_ILLEGAL_ARGUMENT, getExceptionInterceptor());
}
try {
setupStreamingTimeout(locallyScopedConn);
if (this.doEscapeProcessing) {
Object escapedSqlResult = EscapeProcessor.escapeSQL(sql, this.session.getServerSession().getSessionTimeZone(),
this.session.getServerSession().getCapabilities().serverSupportsFracSecs(),
this.session.getServerSession().isServerTruncatesFracSecs(), getExceptionInterceptor());
sql = escapedSqlResult instanceof String ? (String) escapedSqlResult : ((EscapeProcessorResult) escapedSqlResult).escapedSql;
}
CachedResultSetMetaData cachedMetaData = null;
ResultSetInternalMethods rs = null;
this.batchedGeneratedKeys = null;
if (useServerFetch()) {
rs = createResultSetUsingServerFetch(sql);
} else {
CancelQueryTask timeoutTask = null;
String oldDb = null;
try {
timeoutTask = startQueryTimer(this, getTimeoutInMillis());
if (!locallyScopedConn.getDatabase().equals(getCurrentDatabase())) {
oldDb = locallyScopedConn.getDatabase();
locallyScopedConn.setDatabase(getCurrentDatabase());
}
// Check if we have cached metadata for this query...
if (locallyScopedConn.getPropertySet().getBooleanProperty(PropertyKey.cacheResultSetMetadata).getValue()) {
cachedMetaData = locallyScopedConn.getCachedMetaData(sql);
}
// Only apply max_rows to selects
locallyScopedConn.setSessionMaxRows(isResultSetProducingQuery(sql) ? this.maxRows : -1);
statementBegins();
rs = ((NativeSession) locallyScopedConn.getSession()).execSQL(this, sql, this.maxRows, null, meetsConditionsForStreamingResultSet(),
getResultSetFactory(), cachedMetaData, false);
if (timeoutTask != null) {
stopQueryTimer(timeoutTask, true, true);
timeoutTask = null;
}
} catch (CJTimeoutException | OperationCancelledException e) {
throw SQLExceptionsMapping.translateException(e, this.exceptionInterceptor);
} finally {
stopQueryTimer(timeoutTask, false, false);
if (oldDb != null) {
locallyScopedConn.setDatabase(oldDb);
}
}
}
if (rs != null) {
this.lastInsertId = rs.getUpdateID();
this.results = rs;
rs.setFirstCharOfQuery(QueryInfo.firstCharOfStatementUc(sql, this.session.getServerSession().isNoBackslashEscapesSet()));
if (rs.hasRows()) {
if (cachedMetaData != null) {
locallyScopedConn.initializeResultsMetadataFromCache(sql, cachedMetaData, this.results);
} else if (this.session.getPropertySet().getBooleanProperty(PropertyKey.cacheResultSetMetadata).getValue()) {
locallyScopedConn.initializeResultsMetadataFromCache(sql, null /* will be created */, this.results);
}
}
}
return rs != null && rs.hasRows();
} finally {
this.query.getStatementExecuting().set(false);
}
} catch (Throwable t) {
span.setError(t);
throw t;
} finally {
span.end();
}
} finally {
connectionLock.unlock();
}
}