Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cross-database data source confusion #19939

Merged
merged 1 commit into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ public final class JDBCBackendConnection implements BackendConnection<Void>, Exe

@Override
public List<Connection> getConnections(final String dataSourceName, final int connectionSize, final ConnectionMode connectionMode) throws SQLException {
Preconditions.checkNotNull(connectionSession.getDatabaseName(), "Current database name is null.");
Collection<Connection> connections;
synchronized (cachedConnections) {
connections = cachedConnections.get(dataSourceName);
connections = cachedConnections.get(connectionSession.getDatabaseName() + "." + dataSourceName);
}
List<Connection> result;
if (connections.size() >= connectionSize) {
Expand All @@ -80,19 +81,18 @@ public List<Connection> getConnections(final String dataSourceName, final int co
List<Connection> newConnections = createNewConnections(dataSourceName, connectionSize - connections.size(), connectionMode);
result.addAll(newConnections);
synchronized (cachedConnections) {
cachedConnections.putAll(dataSourceName, newConnections);
cachedConnections.putAll(connectionSession.getDatabaseName() + "." + dataSourceName, newConnections);
}
} else {
result = createNewConnections(dataSourceName, connectionSize, connectionMode);
synchronized (cachedConnections) {
cachedConnections.putAll(dataSourceName, result);
cachedConnections.putAll(connectionSession.getDatabaseName() + "." + dataSourceName, result);
}
}
return result;
}

private List<Connection> createNewConnections(final String dataSourceName, final int connectionSize, final ConnectionMode connectionMode) throws SQLException {
Preconditions.checkNotNull(connectionSession.getDatabaseName(), "Current schema is null.");
List<Connection> result = ProxyContext.getInstance().getBackendDataSource().getConnections(connectionSession.getDatabaseName(), dataSourceName, connectionSize, connectionMode);
setSessionVariablesIfNecessary(result);
for (Connection each : result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public List<Future<? extends SqlClient>> getConnections(final String dataSourceN
private List<Future<? extends SqlClient>> getConnectionsWithTransaction(final String dataSourceName, final int connectionSize) {
Collection<Future<SqlConnection>> connections;
synchronized (cachedConnections) {
connections = cachedConnections.get(dataSourceName);
connections = cachedConnections.get(connectionSession.getDatabaseName() + "." + dataSourceName);
}
List<Future<SqlConnection>> result;
if (connections.size() >= connectionSize) {
Expand All @@ -84,12 +84,12 @@ private List<Future<? extends SqlClient>> getConnectionsWithTransaction(final St
List<Future<SqlConnection>> newConnections = createNewConnections(dataSourceName, connectionSize - connections.size());
result.addAll(newConnections);
synchronized (cachedConnections) {
cachedConnections.putAll(dataSourceName, newConnections);
cachedConnections.putAll(connectionSession.getDatabaseName() + "." + dataSourceName, newConnections);
}
} else {
result = createNewConnections(dataSourceName, connectionSize);
synchronized (cachedConnections) {
cachedConnections.putAll(dataSourceName, result);
cachedConnections.putAll(connectionSession.getDatabaseName() + "." + dataSourceName, result);
}
}
return new ArrayList<>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void assertGetConnectionsWithoutTransactions() throws SQLException {
List<Connection> fetchedConnections = backendConnection.getConnections("ds1", 1, null);
assertThat(fetchedConnections.size(), is(1));
assertTrue(fetchedConnections.contains(connections.get(0)));
assertConnectionsCached("ds1", connections);
assertConnectionsCached(connectionSession.getDatabaseName() + ".ds1", connections);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class MockConnectionUtil {
@SneakyThrows(ReflectiveOperationException.class)
static void setCachedConnections(final JDBCBackendConnection backendConnection, final String dataSourceName, final int connectionSize) {
Multimap<String, Connection> cachedConnections = HashMultimap.create();
cachedConnections.putAll(dataSourceName, mockNewConnections(connectionSize));
cachedConnections.putAll(backendConnection.getConnectionSession().getDatabaseName() + "." + dataSourceName, mockNewConnections(connectionSize));
Field field = backendConnection.getClass().getDeclaredField("cachedConnections");
field.setAccessible(true);
field.set(backendConnection, cachedConnections);
Expand Down