Skip to content

Commit b81bbc9

Browse files
authored
fixes #2323 always reset schema upon connection retrieval (#2346)
1 parent ee5328d commit b81bbc9

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,8 @@ public String getSchema() throws SQLException
505505
public void setSchema(String schema) throws SQLException
506506
{
507507
delegate.setSchema(schema);
508-
var appliedSchema = delegate.getSchema();
509-
if (appliedSchema != null && !appliedSchema.equals(dbschema)) {
510-
dirtyBits |= DIRTY_BIT_SCHEMA;
511-
}
512-
dbschema = appliedSchema;
508+
dbschema = delegate.getSchema();
509+
dirtyBits |= DIRTY_BIT_SCHEMA;
513510
}
514511

515512
/** {@inheritDoc} */

src/test/java/com/zaxxer/hikari/pool/PostgresTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@
4242
public class PostgresTest
4343
{
4444
private static final DockerImageName IMAGE_NAME = DockerImageName.parse("postgres:16");
45+
private static final String SCHEMA_NAME = "test";
4546

4647
private PostgreSQLContainer<?> postgres;
4748

4849
@Before
4950
public void beforeTest() {
50-
postgres = new PostgreSQLContainer<>(IMAGE_NAME);
51+
postgres = new PostgreSQLContainer<>(IMAGE_NAME).withInitScript("postgres_init_script.sql");
5152
postgres.start();
5253
}
5354

@@ -139,6 +140,31 @@ public void testCredentialRotation()
139140
exerciseConfig(config, 3);
140141
}
141142

143+
@Test
144+
public void testSchema() throws Exception {
145+
HikariConfig config = createConfig(postgres);
146+
config.setMinimumIdle(1);
147+
config.setMaximumPoolSize(1);
148+
config.setSchema(SCHEMA_NAME);
149+
150+
try (HikariDataSource ds = new HikariDataSource(config)) {
151+
assertTrue(ds.isRunning());
152+
try (Connection connection = ds.getConnection()) {
153+
// The connection should be using the schema specified in the config
154+
assertEquals(SCHEMA_NAME, connection.getSchema());
155+
// Explicitly set the schema to an invalid schema
156+
connection.setSchema("invalid");
157+
// The connection should have a null schema after setting to an invalid schema
158+
assertNull(connection.getSchema());
159+
}
160+
try (Connection connection = ds.getConnection()) {
161+
// The connection should be reset to the schema specified in the config
162+
assertEquals(SCHEMA_NAME, connection.getSchema());
163+
}
164+
assertTrue(ds.isRunning());
165+
}
166+
}
167+
142168
static private void exerciseConfig(HikariConfig config, int numThreads) {
143169
try (final HikariDataSource ds = new HikariDataSource(config)) {
144170
assertTrue(ds.isRunning());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE SCHEMA IF NOT EXISTS test;

0 commit comments

Comments
 (0)