-
Notifications
You must be signed in to change notification settings - Fork 72
Improve internal connection pool key defaults #432
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
Changes from 2 commits
8016c28
004c0bf
cbb5592
1aa0026
d33979c
89609c4
88720f0
485214f
54afad9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,7 +120,7 @@ public Connection connect( | |
| @NonNull Properties props) | ||
| throws SQLException { | ||
| final HikariDataSource ds = databasePools.computeIfAbsent( | ||
| poolMapping.getKey(hostSpec, props), | ||
| getPoolKey(hostSpec, props), | ||
| url -> createHikariDataSource(protocol, hostSpec, props) | ||
| ); | ||
|
|
||
|
|
@@ -140,6 +140,14 @@ public Connection connect( | |
| return null; | ||
| } | ||
|
|
||
| // The pool key should always be retrieved using this method, because the username/password | ||
| // must always be included to avoid sharing privileged connections with other users. | ||
| private String getPoolKey(HostSpec hostSpec, Properties props) { | ||
| return poolMapping.getKey(hostSpec, props) | ||
| + props.getProperty(PropertyDefinition.USER.name) | ||
| + props.getProperty(PropertyDefinition.PASSWORD.name); | ||
|
||
| } | ||
|
|
||
| @Override | ||
| public void releaseResources() { | ||
| databasePools.forEach((String url, HikariDataSource ds) -> ds.close()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,10 +27,12 @@ | |
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| import com.zaxxer.hikari.HikariConfig; | ||
| import com.zaxxer.hikari.pool.HikariPool; | ||
| import integration.refactored.DatabaseEngine; | ||
| import integration.refactored.DatabaseEngineDeployment; | ||
| import integration.refactored.DriverHelper; | ||
| import integration.refactored.TestEnvironmentFeatures; | ||
| import integration.refactored.TestEnvironmentInfo; | ||
| import integration.refactored.TestInstanceInfo; | ||
| import integration.refactored.container.ConnectionStringHelper; | ||
| import integration.refactored.container.ProxyHelper; | ||
|
|
@@ -597,7 +599,7 @@ public void test_pooledConnection_reuseCachedConnection() throws SQLException { | |
| protected static HikariConfig getHikariConfig(HostSpec hostSpec, Properties props) { | ||
| final HikariConfig config = new HikariConfig(); | ||
| config.setMaximumPoolSize(1); | ||
| config.setInitializationFailTimeout(75000); | ||
| config.setInitializationFailTimeout(20000); | ||
| return config; | ||
| } | ||
|
|
||
|
|
@@ -758,4 +760,75 @@ public void test_pooledConnection_failoverInTransaction() | |
| ConnectionProviderManager.resetProvider(); | ||
| } | ||
| } | ||
|
|
||
| @TestTemplate | ||
| @EnableOnTestFeature(TestEnvironmentFeatures.FAILOVER_SUPPORTED) | ||
|
||
| public void test_pooledConnection_differentUsers() throws SQLException { | ||
| Properties privilegedUserProps = getProps(); | ||
|
|
||
| Properties privilegedUserWithWrongPasswordProps = getProps(); | ||
| privilegedUserWithWrongPasswordProps.setProperty(PropertyDefinition.PASSWORD.name, "bogus_password"); | ||
|
|
||
| Properties limitedUserProps = getProps(); | ||
| String limitedUserName = "limited_user"; | ||
| String limitedUserPassword = "limited_user"; | ||
| String limitedUserNewDb = "limited_user_db"; | ||
| limitedUserProps.setProperty(PropertyDefinition.USER.name, limitedUserName); | ||
| limitedUserProps.setProperty(PropertyDefinition.PASSWORD.name, limitedUserPassword); | ||
|
|
||
| Properties wrongUserRightPasswordProps = getProps(); | ||
| wrongUserRightPasswordProps.setProperty(PropertyDefinition.USER.name, "bogus_user"); | ||
|
|
||
| final HikariPooledConnectionProvider provider = | ||
| new HikariPooledConnectionProvider(ReadWriteSplittingTests::getHikariConfig); | ||
| ConnectionProviderManager.setConnectionProvider(provider); | ||
|
|
||
| try { | ||
| try (Connection conn = DriverManager.getConnection(ConnectionStringHelper.getWrapperUrl(), | ||
| privilegedUserProps); Statement stmt = conn.createStatement()) { | ||
| stmt.execute("DROP USER IF EXISTS " + limitedUserName); | ||
| auroraUtil.createUser(conn, limitedUserName, limitedUserPassword); | ||
| TestEnvironmentInfo info = TestEnvironment.getCurrent().getInfo(); | ||
| DatabaseEngine engine = info.getRequest().getDatabaseEngine(); | ||
| if (DatabaseEngine.MYSQL.equals(engine)) { | ||
| String db = info.getDatabaseInfo().getDefaultDbName(); | ||
| // MySQL needs this extra command to allow the limited user access to the database | ||
| stmt.execute("GRANT ALL PRIVILEGES ON " + db + ".* to " + limitedUserName); | ||
| } | ||
| } | ||
|
|
||
| try (final Connection conn = DriverManager.getConnection( | ||
| ConnectionStringHelper.getWrapperUrl(), limitedUserProps); | ||
| Statement stmt = conn.createStatement()) { | ||
| assertThrows(SQLException.class, | ||
| () -> stmt.execute("CREATE DATABASE " + limitedUserNewDb)); | ||
| } | ||
|
|
||
| assertThrows( | ||
| HikariPool.PoolInitializationException.class, () -> { | ||
| try (final Connection conn = DriverManager.getConnection( | ||
| ConnectionStringHelper.getWrapperUrl(), privilegedUserWithWrongPasswordProps)) { | ||
| // Do nothing (close connection automatically) | ||
| } | ||
| }); | ||
|
|
||
| assertThrows( | ||
| HikariPool.PoolInitializationException.class, () -> { | ||
| try (final Connection conn = DriverManager.getConnection( | ||
| ConnectionStringHelper.getWrapperUrl(), wrongUserRightPasswordProps)) { | ||
| // Do nothing (close connection automatically) | ||
| } | ||
| }); | ||
| } finally { | ||
| try (Connection conn = DriverManager.getConnection(ConnectionStringHelper.getWrapperUrl(), | ||
| privilegedUserProps); | ||
| Statement stmt = conn.createStatement()) { | ||
| stmt.execute("DROP DATABASE IF EXISTS " + limitedUserNewDb); | ||
| stmt.execute("DROP USER IF EXISTS " + limitedUserName); | ||
| } | ||
|
|
||
| ConnectionProviderManager.releaseResources(); | ||
| ConnectionProviderManager.resetProvider(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.