Skip to content

Commit

Permalink
fix issue #308
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiyv-improving committed Nov 14, 2022
1 parent fa60746 commit 57630da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/user-impl/java/com/mysql/cj/jdbc/ha/ConnectionProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,28 @@ class JdbcInterfaceProxy implements InvocationHandler {
this.invokeOn = toInvokeOn;
}

/**
* Special handling of method calls that can be handled without making an explicit invocation against the connection
* underlying this proxy. See {@link #isDirectExecute(String)}
*
* @param methodName The name of the method being called
* @param args The argument parameters of the method that is being called
* @return The results of the special method handling, according to which method was called
*/
private Object executeMethodDirectly(String methodName, Object[] args) {
if (METHOD_EQUALS.equals(methodName) && args != null && args.length > 0 && args[0] != null) {
return args[0].equals(this);
}

if (METHOD_HASH_CODE.equals(methodName)) {
return this.hashCode();
}

// should never reach this statement, as the conditions in this method were previously checked in the method
// calling this class using the isForwardingRequired method
return null;
}

public synchronized Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
if (isDirectExecute(methodName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -591,4 +592,19 @@ public void test_ConnectionStringWithValuelessParameter() throws SQLException {
assertTrue(conn.isValid(5));
conn.close();
}

@Test
public void test_PreparedStatementHashCodes() throws SQLException, IOException {
final Connection conn = connectToInstance(MYSQL_INSTANCE_1_URL + PROXIED_DOMAIN_NAME_SUFFIX, MYSQL_PROXY_PORT);
assertTrue(conn.isValid(5));

PreparedStatement prepStmt1 = conn.prepareCall("select 1");
PreparedStatement prepStmt2 = conn.prepareCall("select 1");

assertNotEquals(prepStmt1.hashCode(), prepStmt2.hashCode());
assertNotEquals(prepStmt1, prepStmt2);

conn.close();
}

}

0 comments on commit 57630da

Please sign in to comment.