272
294
private boolean serverNeedsResetBeforeEachExecution;
297
* Creates a prepared statement instance -- We need to provide factory-style
298
* methods so we can support both JDBC3 (and older) and JDBC4 runtimes,
299
* otherwise the class verifier complains when it tries to load JDBC4-only
300
* interface classes that are present in JDBC4 method signatures.
303
protected static ServerPreparedStatement getInstance(ConnectionImpl conn,
304
String sql, String catalog, int resultSetType,
305
int resultSetConcurrency) throws SQLException {
306
if (!Util.isJdbc4()) {
307
return new ServerPreparedStatement(conn, sql, catalog,
308
resultSetType, resultSetConcurrency);
312
return (ServerPreparedStatement) JDBC_4_SPS_CTOR.newInstance(new Object[] { conn,
313
sql, catalog, Constants.integerValueOf(resultSetType),
314
Constants.integerValueOf(resultSetConcurrency) });
315
} catch (IllegalArgumentException e) {
316
throw new SQLException(e.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
317
} catch (InstantiationException e) {
318
throw new SQLException(e.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
319
} catch (IllegalAccessException e) {
320
throw new SQLException(e.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
321
} catch (InvocationTargetException e) {
322
Throwable target = e.getTargetException();
324
if (target instanceof SQLException) {
325
throw (SQLException)target;
328
throw new SQLException(target.toString(), SQLError.SQL_STATE_GENERAL_ERROR);
275
333
* Creates a new ServerPreparedStatement object.
878
958
if (this.connection.getAutoGenerateTestcaseScript()) {
879
959
dumpCloseForTestcase();
882
synchronized (this.connection.getMutex()) {
885
// Don't communicate with the server if we're being
886
// called from the finalizer...
888
// This will leak server resources, but if we don't do this,
889
// we'll deadlock (potentially, because there's no guarantee
890
// when, what order, and what concurrency finalizers will be
891
// called with). Well-behaved programs won't rely on finalizers
892
// to clean up their statements.
895
SQLException exceptionDuringClose = null;
898
if (calledExplicitly) {
963
// Don't communicate with the server if we're being
964
// called from the finalizer...
966
// This will leak server resources, but if we don't do this,
967
// we'll deadlock (potentially, because there's no guarantee
968
// when, what order, and what concurrency finalizers will be
969
// called with). Well-behaved programs won't rely on finalizers
970
// to clean up their statements.
973
SQLException exceptionDuringClose = null;
975
if (calledExplicitly && !this.connection.isClosed()) {
976
synchronized (this.connection.getMutex()) {
901
979
MysqlIO mysql = this.connection.getIO();
903
981
Buffer packet = mysql.getSharedSendPacket();
905
983
packet.writeByte((byte) MysqlDefs.COM_CLOSE_STATEMENT);
906
984
packet.writeLong(this.serverStatementId);
908
986
mysql.sendCommand(MysqlDefs.COM_CLOSE_STATEMENT, null,
909
987
packet, true, null);
910
988
} catch (SQLException sqlEx) {
911
989
exceptionDuringClose = sqlEx;
916
super.realClose(calledExplicitly, closeOpenResults);
918
clearParametersInternal(false);
919
this.parameterBindings = null;
921
this.parameterFields = null;
922
this.resultFields = null;
924
if (exceptionDuringClose != null) {
925
throw exceptionDuringClose;
994
super.realClose(calledExplicitly, closeOpenResults);
996
clearParametersInternal(false);
997
this.parameterBindings = null;
999
this.parameterFields = null;
1000
this.resultFields = null;
1002
if (exceptionDuringClose != null) {
1003
throw exceptionDuringClose;
1169
1250
Buffer resultPacket = mysql.sendCommand(MysqlDefs.COM_EXECUTE,
1170
1251
null, packet, false, null);
1253
long queryEndTime = 0L;
1255
if (logSlowQueries || gatherPerformanceMetrics || this.profileSQL) {
1256
queryEndTime = mysql.getCurrentTimeNanosOrMillis();
1172
1259
if (timeoutTask != null) {
1173
1260
timeoutTask.cancel();
1262
if (timeoutTask.caughtWhileCancelling != null) {
1263
throw timeoutTask.caughtWhileCancelling;
1174
1266
timeoutTask = null;
1177
if (this.wasCancelled) {
1178
this.wasCancelled = false;
1179
throw new MySQLTimeoutException();
1182
this.connection.incrementNumberOfPreparedExecutes();
1184
if (this.connection.getProfileSql()) {
1185
this.eventSink = ProfileEventSink.getInstance(this.connection);
1187
this.eventSink.consumeEvent(new ProfilerEvent(
1188
ProfilerEvent.TYPE_EXECUTE, "", this.currentCatalog, //$NON-NLS-1$
1189
this.connectionId, this.statementId, -1, System
1190
.currentTimeMillis(), (int) (System
1191
.currentTimeMillis() - begin), null,
1192
new Throwable(), truncateQueryToLog(asSql(true))));
1195
com.mysql.jdbc.ResultSet rs = mysql.readAllResults(this,
1196
maxRowsToRetrieve, this.resultSetType,
1197
this.resultSetConcurrency, createStreamingResultSet,
1198
this.currentCatalog, resultPacket, true, this.fieldCount,
1269
synchronized (this.cancelTimeoutMutex) {
1270
if (this.wasCancelled) {
1271
SQLException cause = null;
1273
if (this.wasCancelledByTimeout) {
1274
cause = new MySQLTimeoutException();
1276
cause = new MySQLStatementCancelledException();
1279
resetCancelledState();
1285
boolean queryWasSlow = false;
1202
if (!createStreamingResultSet &&
1203
this.serverNeedsResetBeforeEachExecution) {
1204
serverResetStatement(); // clear any long data...
1207
this.sendTypesToServer = false;
1210
if (this.connection.getLogSlowQueries()
1211
|| this.connection.getGatherPerformanceMetrics()) {
1212
long elapsedTime = System.currentTimeMillis() - begin;
1214
if (this.connection.getLogSlowQueries()
1215
&& (elapsedTime >= this.connection
1216
.getSlowQueryThresholdMillis())) {
1287
if (logSlowQueries || gatherPerformanceMetrics) {
1288
long elapsedTime = queryEndTime - begin;
1290
if (logSlowQueries) {
1291
if (this.useAutoSlowLog) {
1292
queryWasSlow = elapsedTime > this.connection.getSlowQueryThresholdMillis();
1294
queryWasSlow = this.connection.isAbonormallyLongQuery(elapsedTime);
1296
this.connection.reportQueryTime(elapsedTime);
1217
1302
StringBuffer mesgBuf = new StringBuffer(
1218
1303
48 + this.originalSql.length());
1219
1304
mesgBuf.append(Messages
1220
1305
.getString("ServerPreparedStatement.15")); //$NON-NLS-1$
1221
mesgBuf.append(this.connection
1222
.getSlowQueryThresholdMillis());
1306
mesgBuf.append(mysql.getSlowQueryThreshold());
1223
1307
mesgBuf.append(Messages
1224
1308
.getString("ServerPreparedStatement.15a")); //$NON-NLS-1$
1225
1309
mesgBuf.append(elapsedTime);
1226
1310
mesgBuf.append(Messages
1227
1311
.getString("ServerPreparedStatement.16")); //$NON-NLS-1$
1229
1313
mesgBuf.append("as prepared: ");
1230
1314
mesgBuf.append(this.originalSql);
1231
1315
mesgBuf.append("\n\n with parameters bound:\n\n");
1232
1316
mesgBuf.append(asSql(true));
1234
this.connection.getLog().logWarn(mesgBuf.toString());
1236
if (this.connection.getExplainSlowQueries()) {
1237
String queryAsString = asSql(true);
1239
mysql.explainSlowQuery(queryAsString.getBytes(),
1319
.consumeEvent(new ProfilerEvent(
1320
ProfilerEvent.TYPE_SLOW_QUERY,
1321
"", this.currentCatalog, this.connection.getId(), //$NON-NLS-1$
1322
getId(), 0, System.currentTimeMillis(),
1324
.getQueryTimingUnits(), null,
1325
new Throwable(), mesgBuf.toString()));
1244
if (this.connection.getGatherPerformanceMetrics()) {
1328
if (gatherPerformanceMetrics) {
1245
1329
this.connection.registerQueryExecutionTime(elapsedTime);
1333
this.connection.incrementNumberOfPreparedExecutes();
1335
if (this.profileSQL) {
1336
this.eventSink = ProfileEventSink
1337
.getInstance(this.connection);
1339
this.eventSink.consumeEvent(new ProfilerEvent(
1340
ProfilerEvent.TYPE_EXECUTE,
1341
"", this.currentCatalog, //$NON-NLS-1$
1342
this.connectionId, this.statementId, -1, System
1343
.currentTimeMillis(), (int) (mysql
1344
.getCurrentTimeNanosOrMillis() - begin),
1345
mysql.getQueryTimingUnits(), null, new Throwable(),
1346
truncateQueryToLog(asSql(true))));
1349
com.mysql.jdbc.ResultSetInternalMethods rs = mysql.readAllResults(this,
1350
maxRowsToRetrieve, this.resultSetType,
1351
this.resultSetConcurrency, createStreamingResultSet,
1352
this.currentCatalog, resultPacket, true, this.fieldCount,
1355
if (this.profileSQL) {
1356
long fetchEndTime = mysql.getCurrentTimeNanosOrMillis();
1358
this.eventSink.consumeEvent(new ProfilerEvent(
1359
ProfilerEvent.TYPE_FETCH,
1360
"", this.currentCatalog, this.connection.getId(), //$NON-NLS-1$
1361
getId(), 0 /* FIXME rs.resultId */, System.currentTimeMillis(),
1362
(fetchEndTime - queryEndTime), mysql
1363
.getQueryTimingUnits(), null,
1364
new Throwable(), null));
1367
if (queryWasSlow && this.connection.getExplainSlowQueries()) {
1368
String queryAsString = asSql(true);
1370
mysql.explainSlowQuery(queryAsString.getBytes(),
1374
if (!createStreamingResultSet &&
1375
this.serverNeedsResetBeforeEachExecution) {
1376
serverResetStatement(); // clear any long data...
1380
this.sendTypesToServer = false;
1249
1383
if (mysql.hadWarnings()) {
1250
1384
mysql.scanForAndThrowDataTruncation();