Skip to content

Commit 02cc8d1

Browse files
authored
Merge pull request #69 from ebean-orm/feature/use-connectionSetSchema
When schema specified, change to use connection.setSchema() rather than connection Properties
2 parents 5d0a0fb + 1c74ffc commit 02cc8d1

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

ebean-datasource-api/src/main/java/io/ebean/datasource/PostgresInitDatabase.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ public class PostgresInitDatabase implements InitDatabase {
1717
public void run(Connection connection, DataSourceConfig config) throws SQLException {
1818
String username = config.getUsername();
1919
String password = config.getPassword();
20-
log.log(System.Logger.Level.INFO, "Creating schema and role for {0}", username);
21-
execute(connection, String.format("create schema if not exists %s", username));
20+
String schema = config.getSchema();
21+
if (schema == null) {
22+
schema = username;
23+
}
24+
log.log(System.Logger.Level.INFO, "Creating schema {0} role {1}", schema, username);
25+
execute(connection, String.format("create schema if not exists %s", schema));
2226
execute(connection, String.format("create role %s with login password '%s'", username, password));
23-
execute(connection, String.format("grant all on schema %s to %s", username, username));
27+
execute(connection, String.format("grant all on schema %s to %s", schema, username));
2428
}
2529

2630
private void execute(Connection connection, String sql) throws SQLException {

ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ final class ConnectionPool implements DataSourcePool {
4343
private final String driver;
4444
private final String url;
4545
private final String user;
46+
private final String schema;
4647
private final String heartbeatsql;
4748
private final int heartbeatFreqSecs;
4849
private final int heartbeatTimeoutSeconds;
@@ -114,6 +115,7 @@ final class ConnectionPool implements DataSourcePool {
114115
this.applicationName = params.getApplicationName();
115116
this.clientInfo = params.getClientInfo();
116117
this.queue = new PooledConnectionQueue(this);
118+
this.schema = params.getSchema();
117119
this.user = params.getUsername();
118120
if (user == null) {
119121
throw new DataSourceConfigurationException("DataSource user is not set? url is [" + url + "]");
@@ -125,10 +127,6 @@ final class ConnectionPool implements DataSourcePool {
125127
this.connectionProps = new Properties();
126128
this.connectionProps.setProperty("user", user);
127129
this.connectionProps.setProperty("password", pw);
128-
final String schema = params.getSchema();
129-
if (schema != null) {
130-
this.connectionProps.setProperty("schema", schema);
131-
}
132130

133131
Map<String, String> customProperties = params.getCustomProperties();
134132
if (customProperties != null) {
@@ -427,6 +425,9 @@ private void initConnection(Connection conn) throws SQLException {
427425
if (readOnly) {
428426
conn.setReadOnly(true);
429427
}
428+
if (schema != null) {
429+
conn.setSchema(schema);
430+
}
430431
if (applicationName != null) {
431432
try {
432433
conn.setClientInfo("ApplicationName", applicationName);

ebean-datasource/src/test/java/io/ebean/datasource/test/PostgresInitTest.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,39 @@ void test_with_clientInfo() throws SQLException {
7777
}
7878

7979
@Test
80-
void test_with_applicationName() throws SQLException {
80+
void test_with_applicationNameAndSchema() throws SQLException {
8181
DataSourceConfig ds = new DataSourceConfig();
8282
ds.setUrl("jdbc:postgresql://127.0.0.1:9999/app");
83-
// our application credentials (typically same as db and schema name with Postgres)
84-
ds.setUsername("app");
85-
ds.setPassword("app_pass");
86-
// database owner credentials used to create the "app" role as needed
87-
ds.setOwnerUsername("db_owner");
88-
ds.setOwnerPassword("test");
83+
ds.setSchema("fred");
84+
ds.setUsername("db_owner");
85+
ds.setPassword("test");
8986
ds.setApplicationName("my-application-name");
9087

9188
DataSourcePool pool = DataSourceFactory.create("app", ds);
9289
try {
9390
try (Connection connection = pool.getConnection()) {
94-
try (PreparedStatement statement = connection.prepareStatement("create table if not exists app.my_table (acol integer);")) {
91+
try (PreparedStatement statement = connection.prepareStatement("create schema if not exists fred;")) {
9592
statement.execute();
9693
}
9794
connection.commit();
95+
try (PreparedStatement statement = connection.prepareStatement("create table if not exists fred_table (acol integer);")) {
96+
statement.execute();
97+
}
98+
try (PreparedStatement statement = connection.prepareStatement("insert into fred_table (acol) values (?);")) {
99+
statement.setInt(1, 42);
100+
int rows = statement.executeUpdate();
101+
assertThat(rows).isEqualTo(1);
102+
}
103+
try (PreparedStatement statement = connection.prepareStatement("select acol from fred.fred_table")) {
104+
try (ResultSet resultSet = statement.executeQuery()) {
105+
while(resultSet.next()) {
106+
int res = resultSet.getInt(1);
107+
assertThat(res).isEqualTo(42);
108+
}
109+
}
110+
}
111+
connection.commit();
112+
98113
try (PreparedStatement statement = connection.prepareStatement("select application_name from pg_stat_activity where usename = ?")) {
99114
statement.setString(1, "app");
100115
try (ResultSet rset = statement.executeQuery()) {

0 commit comments

Comments
 (0)