Skip to content

Commit

Permalink
Cross-library data source confusion repair (#19939)
Browse files Browse the repository at this point in the history
  • Loading branch information
natehuangting authored Aug 7, 2022
1 parent 20cc50a commit 1adda16
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
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

0 comments on commit 1adda16

Please sign in to comment.