Skip to content

Commit f5ef643

Browse files
committed
[CONJ-1111] ensure using same ip in place of DNS when creating a connection to kill running query
1 parent 75a21de commit f5ef643

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

src/main/java/org/mariadb/jdbc/internal/protocol/AbstractConnectProtocol.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,9 +1433,18 @@ public HostAddress getHostAddress() {
14331433
return currentHost;
14341434
}
14351435

1436+
public String getSocketIp() {
1437+
return this.socket.getInetAddress() == null
1438+
? null
1439+
: this.socket.getInetAddress().getHostAddress();
1440+
}
1441+
14361442
public void setHostAddress(HostAddress host) {
14371443
this.currentHost = host;
1438-
this.readOnly = ParameterConstant.TYPE_REPLICA.equals(this.currentHost.type);
1444+
this.readOnly =
1445+
this.currentHost == null
1446+
? false
1447+
: ParameterConstant.TYPE_REPLICA.equals(this.currentHost.type);
14391448
}
14401449

14411450
public String getHost() {

src/main/java/org/mariadb/jdbc/internal/protocol/AbstractQueryProtocol.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@
6969
import java.util.concurrent.locks.ReentrantLock;
7070
import java.util.regex.Pattern;
7171
import javax.sql.rowset.serial.SerialException;
72-
import org.mariadb.jdbc.LocalInfileInterceptor;
73-
import org.mariadb.jdbc.MariaDbConnection;
74-
import org.mariadb.jdbc.MariaDbStatement;
75-
import org.mariadb.jdbc.UrlParser;
72+
import org.mariadb.jdbc.*;
7673
import org.mariadb.jdbc.internal.ColumnType;
7774
import org.mariadb.jdbc.internal.MariaDbServerCapabilities;
7875
import org.mariadb.jdbc.internal.com.read.Buffer;
@@ -1362,7 +1359,13 @@ public void resetDatabase() throws SQLException {
13621359
public void cancelCurrentQuery() throws SQLException {
13631360
try (MasterProtocol copiedProtocol =
13641361
new MasterProtocol(urlParser, new GlobalStateInfo(), new ReentrantLock(), traceCache)) {
1365-
copiedProtocol.setHostAddress(getHostAddress());
1362+
HostAddress currentHost = getHostAddress();
1363+
String currentIp = getSocketIp();
1364+
HostAddress hostAddress =
1365+
currentIp == null
1366+
? currentHost
1367+
: new HostAddress(currentIp, currentHost.port, currentHost.type);
1368+
copiedProtocol.setHostAddress(hostAddress);
13661369
copiedProtocol.connect();
13671370
// no lock, because there is already a query running that possessed the lock.
13681371
copiedProtocol.executeQuery("KILL QUERY " + serverThreadId);

src/test/java/org/mariadb/jdbc/BaseTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public class BaseTest {
9292
protected static String database;
9393
protected static String username;
9494
protected static String password;
95+
protected static String defaultOther;
9596
protected static Options options;
9697
protected static String parameters;
9798
protected static boolean testSingleHost;
@@ -106,7 +107,7 @@ public class BaseTest {
106107
BaseTest.class.getClassLoader().getResourceAsStream("conf.properties")) {
107108
Properties prop = new Properties();
108109
prop.load(inputStream);
109-
String defaultOther;
110+
110111
String val = System.getenv("TEST_REQUIRE_TLS");
111112
defaultOther = get("DB_OTHER", prop);
112113
if ("1".equals(val)) {

src/test/java/org/mariadb/jdbc/DriverTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,65 @@ public void namedPipe() {
14391439
}
14401440
}
14411441

1442+
@Test
1443+
public void namedPipeCancel() {
1444+
try (ResultSet rs =
1445+
sharedConnection
1446+
.createStatement()
1447+
.executeQuery("select @@named_pipe,@@socket,@@named_pipe_full_access_group")) {
1448+
assertTrue(rs.next());
1449+
if (rs.getBoolean(1)) {
1450+
String namedPipeName = rs.getString(2);
1451+
// skip test if no namedPipeName was obtained because then we do not use a socket connection
1452+
Assume.assumeTrue(namedPipeName != null);
1453+
1454+
if (!isMariadbServer() && minVersion(8, 0, 14)) {
1455+
String namedPipeFullAccess = rs.getString(3);
1456+
System.out.println("namedPipeFullAccess:" + namedPipeFullAccess);
1457+
Assume.assumeTrue(namedPipeFullAccess != null && !namedPipeFullAccess.isEmpty());
1458+
}
1459+
String connUrl =
1460+
String.format(
1461+
"jdbc:mariadb:///%s?user=%s&password=%s&%s",
1462+
database, username, password, defaultOther);
1463+
1464+
try (Connection connection =
1465+
DriverManager.getConnection(connUrl + "&pipe=" + namedPipeName)) {
1466+
long start;
1467+
long normalExecutionTime;
1468+
1469+
try (PreparedStatement stmt =
1470+
connection.prepareStatement(
1471+
"select * from information_schema.columns as c1, information_schema.tables, mysql.user LIMIT 50000")) {
1472+
start = System.currentTimeMillis();
1473+
stmt.executeQuery();
1474+
normalExecutionTime = System.currentTimeMillis() - start;
1475+
1476+
start = System.currentTimeMillis();
1477+
stmt.setFetchSize(1);
1478+
stmt.executeQuery();
1479+
stmt.cancel();
1480+
}
1481+
1482+
long interruptedExecutionTime = System.currentTimeMillis() - start;
1483+
1484+
System.out.println(normalExecutionTime);
1485+
System.out.println(interruptedExecutionTime);
1486+
// normalExecutionTime = 1500
1487+
// interruptedExecutionTime = 77
1488+
assertTrue(
1489+
"interruptedExecutionTime:"
1490+
+ interruptedExecutionTime
1491+
+ " normalExecutionTime:"
1492+
+ normalExecutionTime,
1493+
interruptedExecutionTime < normalExecutionTime);
1494+
}
1495+
}
1496+
} catch (SQLException e) {
1497+
// not on windows
1498+
}
1499+
}
1500+
14421501
/**
14431502
* CONJ-435 : "All pipe instances are busy" exception on multiple connections to the same named
14441503
* pipe.

0 commit comments

Comments
 (0)