Skip to content

Commit

Permalink
Fix for Bug#84117 (Bug#25247468), includeThreadNamesAsStatementCommen…
Browse files Browse the repository at this point in the history
…t ignored when using prepared statement.

Change-Id: I2461b194721fab24302fb605b71f139f58004866
  • Loading branch information
fjssilva committed Sep 12, 2024
1 parent 288db53 commit c3c536a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

Version 9.1.0

- Fix for Bug#84117 (Bug#25247468), includeThreadNamesAsStatementComment ignored when using prepared statement.
Thanks to Yyjun Yyjun for his contribution.

- Fix for Bug#101054 (Bug#32544786), Batched Query > maxAllowedPacket size causes an ArrayIndexOutOfBoundsException.

- Fix for Bug#108415 (Bug#34579258), NullPointerException in AbstractQuery::stopQueryTimer.
Expand Down
9 changes: 8 additions & 1 deletion src/main/core-impl/java/com/mysql/cj/NativeSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.mysql.cj.conf.HostInfo;
import com.mysql.cj.conf.PropertyDefinitions.OpenTelemetry;
Expand Down Expand Up @@ -622,7 +624,12 @@ public void setSessionVariables() {

@Override
public String getQueryComment() {
return this.queryComment;
String comment = this.queryComment;
if (getPropertySet().getBooleanProperty(PropertyKey.includeThreadNamesAsStatementComment).getValue()) {
comment = Stream.of(comment, Messages.getString("NativeSession.ThreadNameComment", new String[] { Thread.currentThread().getName() }))
.filter(s -> !StringUtils.isNullOrEmpty(s)).collect(Collectors.joining(", "));
}
return comment;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ public NativePacketPayload buildComQuery(NativePacketPayload sharedPacket, Sessi
@Override
public NativePacketPayload buildComQuery(NativePacketPayload sharedPacket, Session sess, String query, Query callingQuery, String characterEncoding) {
String statementComment = sess.getQueryComment();
if (sess.getPropertySet().getBooleanProperty(PropertyKey.includeThreadNamesAsStatementComment).getValue()) {
statementComment = (statementComment != null ? statementComment + ", " : "") + "java thread: " + Thread.currentThread().getName();
}
byte[] commentAsBytes = StringUtils.getBytes(statementComment, characterEncoding);

QueryAttributesBindings queryAttributesBindings = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ NamedPipeSocketFactory.4=Named pipe path can not be null or empty

NativeCapabilites.001=Unsupported protocol version: {0}. Likely connecting to an X Protocol port.

NativeSession.ThreadNameComment=Java thread: {0}

NonRegisteringDriver.3=Hostname of MySQL Server
NonRegisteringDriver.7=Port number of MySQL Server
NonRegisteringDriver.10=Database name;
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/testsuite/regression/StatementRegressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13862,4 +13862,71 @@ public void testBug101054() throws Exception {
} while (allowMQ = !allowMQ);
}

/**
* Tests fix for Bug#84117 (Bug#25247468), includeThreadNamesAsStatementComment ignored when using prepared statement.
*
* @throws Exception
*/
@Test
public void testBug84117() throws Exception {
String threadName = Thread.currentThread().getName();
try {
Thread.currentThread().setName("TestBug84117.thread");

boolean incThrNm = false;
do {
TestBug84117QueryInterceptor.expectsThreadName = incThrNm;
TestBug84117QueryInterceptor.occurrences = 0;

final Properties props = new Properties();
props.setProperty(PropertyKey.queryInterceptors.getKeyName(), TestBug84117QueryInterceptor.class.getName());
props.setProperty(PropertyKey.includeThreadNamesAsStatementComment.getKeyName(), Boolean.toString(incThrNm));

final String testQuery = "/* TestBug84117.comment */ SELECT 'MySQL Connector/J'";

// Statement:
try (Connection testConn = getConnectionWithProps(props); Statement testStmt = testConn.createStatement()) {
testStmt.executeQuery(testQuery);
}

// Client PreparedStatement:
props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "false");
try (Connection testConn = getConnectionWithProps(props); PreparedStatement testPstmt = testConn.prepareStatement(testQuery)) {
testPstmt.executeQuery();
}

// Server PreparedStatement:
props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true");
try (Connection testConn = getConnectionWithProps(props); PreparedStatement testPstmt = testConn.prepareStatement(testQuery)) {
testPstmt.executeQuery();
}

assertEquals(3, TestBug84117QueryInterceptor.occurrences);
} while (incThrNm = !incThrNm);
} finally {
Thread.currentThread().setName(threadName);
}
}

public static class TestBug84117QueryInterceptor extends BaseQueryInterceptor {

static boolean expectsThreadName = false;
static int occurrences = 0;

@Override
public <T extends Resultset> T preProcess(Supplier<String> sql, Query interceptedQuery) {
String sqlString = sql.get();
if (sqlString.contains("SELECT 'MySQL Connector/J'")) {
if (expectsThreadName) {
assertTrue(sqlString.contains("/* Java thread: TestBug84117.thread */"));
} else {
assertFalse(sqlString.contains("/* Java thread: TestBug84117.thread */"));
}
occurrences++;
}
return super.preProcess(sql, interceptedQuery);
}

}

}

0 comments on commit c3c536a

Please sign in to comment.