-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from FOCONIS/fix-changing-schema-catalog-on-c…
…onnection-pool Resetting Schema and Catalog, when connection returend to pool
- Loading branch information
Showing
3 changed files
with
129 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
ebean-datasource/src/test/java/io/ebean/datasource/pool/SchemaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.ebean.datasource.pool; | ||
|
||
import io.ebean.datasource.DataSourceConfig; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
class SchemaTest { | ||
|
||
private final ConnectionPool pool; | ||
|
||
SchemaTest() { | ||
pool = createPool(); | ||
} | ||
|
||
private ConnectionPool createPool() { | ||
DataSourceConfig config = new DataSourceConfig(); | ||
config.setUrl("jdbc:h2:mem:tests"); | ||
config.setUsername("sa"); | ||
config.setPassword(""); | ||
config.setMinConnections(2); | ||
config.setMaxConnections(4); | ||
|
||
return new ConnectionPool("test", config); | ||
} | ||
|
||
@BeforeEach | ||
void initTables() throws SQLException { | ||
try (Connection conn = pool.getConnection()) { | ||
Statement statement = conn.createStatement(); | ||
statement.execute("CREATE TABLE test (name VARCHAR(255))"); | ||
statement.execute("INSERT INTO test (name) VALUES ('default schema')"); | ||
statement.execute("CREATE SCHEMA SCHEMA1"); | ||
statement.execute("CREATE SCHEMA SCHEMA2"); | ||
conn.commit(); | ||
} | ||
try (Connection conn = pool.getConnection()) { | ||
String orig = conn.getSchema(); | ||
conn.setSchema("SCHEMA1"); | ||
Statement statement = conn.createStatement(); | ||
statement.execute("CREATE TABLE test (name VARCHAR(255))"); | ||
statement.execute("INSERT INTO test (name) VALUES ('schema1')"); | ||
conn.setSchema("SCHEMA2"); | ||
statement.execute("CREATE TABLE test (name VARCHAR(255))"); | ||
statement.execute("INSERT INTO test (name) VALUES ('schema2')"); | ||
conn.setSchema(orig); | ||
conn.commit(); | ||
} | ||
} | ||
|
||
@AfterEach | ||
void after() { | ||
pool.shutdown(); | ||
} | ||
|
||
@Test | ||
void getConnectionWithSchemaSwitch() throws SQLException { | ||
try (Connection conn = pool.getConnection()) { | ||
Statement statement = conn.createStatement(); | ||
ResultSet rs = statement.executeQuery("SELECT name FROM test"); | ||
assertThat(rs.next()).isTrue(); | ||
assertThat(rs.getString("name")).isEqualTo("default schema"); | ||
} | ||
try (Connection conn = pool.getConnection()) { | ||
conn.setSchema("SCHEMA1"); | ||
Statement statement = conn.createStatement(); | ||
ResultSet rs = statement.executeQuery("SELECT name FROM test"); | ||
assertThat(rs.next()).isTrue(); | ||
assertThat(rs.getString("name")).isEqualTo("schema1"); | ||
} | ||
try (Connection conn = pool.getConnection()) { | ||
conn.setSchema("SCHEMA2"); | ||
Statement statement = conn.createStatement(); | ||
ResultSet rs = statement.executeQuery("SELECT name FROM test"); | ||
assertThat(rs.next()).isTrue(); | ||
assertThat(rs.getString("name")).isEqualTo("schema2"); | ||
} | ||
try (Connection conn = pool.getConnection()) { | ||
Statement statement = conn.createStatement(); | ||
ResultSet rs = statement.executeQuery("SELECT name FROM test"); | ||
assertThat(rs.next()).isTrue(); | ||
assertThat(rs.getString("name")).isEqualTo("default schema"); | ||
} | ||
} | ||
} |