Skip to content

Commit

Permalink
Make sure PG connection pools are unique for a set of connection para…
Browse files Browse the repository at this point in the history
…meters

Signed-off-by: Gabriel Roldan <groldan@boundlessgeo.com>
  • Loading branch information
Gabriel Roldan authored and Erik Merkle committed May 17, 2017
1 parent 786e8bd commit a4b2dc2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

class DataSourceManager extends ConnectionManager<Environment, DataSource> {
class DataSourceManager extends ConnectionManager<Environment.ConnectionConfig, DataSource> {

private static final Logger LOG = LoggerFactory.getLogger(DataSourceManager.class);

@Override
protected DataSource connect(Environment config) {
protected DataSource connect(Environment.ConnectionConfig config) {
HikariConfig hc = new HikariConfig();
hc.setConnectionInitSql("SELECT NOW()");
// no need to set a validation query, connections auto validate
// hc.setConnectionTestQuery("SELECT NOW()");
hc.setDriverClassName("org.postgresql.Driver");

final String jdbcUrl = getUrl(config.connectionConfig);
final String jdbcUrl = getUrl(config);
hc.setJdbcUrl(jdbcUrl);

hc.setMaximumPoolSize(10);
Expand All @@ -52,10 +52,13 @@ protected DataSource connect(Environment config) {

LOG.debug("Connecting to " + jdbcUrl + " as user " + config.getUser());
HikariDataSource ds = new HikariDataSource(hc);

final String configTable = (config.getTablePrefix() == null
? TableNames.DEFAULT_TABLE_PREFIX : config.getTablePrefix()) + "config";
try (Connection c = ds.getConnection()) {
final String sql = format(
"SELECT value FROM %s WHERE repository = ? AND section = ? AND key = ?",
config.getTables().config());
configTable);
String[] maxConnections = Environment.KEY_MAX_CONNECTIONS.split("\\.");
String section = maxConnections[0];
String key = maxConnections[1];
Expand All @@ -67,7 +70,8 @@ protected DataSource connect(Environment config) {

try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
ds.setMaximumPoolSize(Integer.parseInt(rs.getString(1)));
String globalMaxConnections = rs.getString(1);
ds.setMaximumPoolSize(Integer.parseInt(globalMaxConnections));
}
}
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;

/**
* <p>
Expand All @@ -35,6 +36,8 @@ public class Environment {

public static final String KEY_DB_PORT = "postgres.port";

public static final int DEFAULT_DB_PORT = 5432;

public static final String KEY_DB_SCHEMA = "postgres.schema";

public static final String KEY_DB_NAME = "postgres.database";
Expand Down Expand Up @@ -71,9 +74,9 @@ public class Environment {

static class ConnectionConfig {

private final String user;
private @Nullable final String user;

private final String password;
private @Nullable final String password;

private final String databaseName;

Expand All @@ -83,14 +86,60 @@ static class ConnectionConfig {

private final String server;

private @Nullable final String tablePrefix;

ConnectionConfig(final String server, final int portNumber, final String databaseName,
final String schema, final String user, final String password) {
final String schema, @Nullable final String user, @Nullable final String password,
@Nullable String tablePrefix) {
this.server = server;
this.portNumber = portNumber;
this.databaseName = databaseName;
this.schema = schema;
this.user = user;
this.password = password;
this.tablePrefix = tablePrefix;
}

public URI toURI() {
return toURIInternal(null);
}

public URI toURI(final String repositoryName) {
Preconditions.checkNotNull(repositoryName);
return toURIInternal(repositoryName);
}

private URI toURIInternal(final @Nullable String repositoryName) {

// postgresql://<server>:<port>/<database>/<schema>[/<repoid>]?user=<username>][&password=<pwd>][&tablePrefix=<prefix>]
StringBuilder sb = new StringBuilder("postgresql://").append(server).append(":")
.append(portNumber).append("/").append(databaseName).append("/").append(schema);

if (repositoryName != null) {
sb.append("/").append(repositoryName);
}
StringBuilder args = new StringBuilder();
if (this.user != null) {
args.append("user=").append(this.user);
}
if (password != null) {
args.append(args.length() > 0 ? "&password=" : "password=").append(password);
}
if (tablePrefix != null) {
args.append(args.length() > 0 ? "&tablePrefix=" : "tablePrefix=")
.append(tablePrefix);
}
if (args.length() > 0) {
sb.append("?").append(args);
}

URI repoURI = null;
try {
repoURI = new URI(sb.toString());
} catch (URISyntaxException e) {
Throwables.propagate(e);
}
return repoURI;
}

@Override
Expand All @@ -102,23 +151,25 @@ public boolean equals(Object o) {
return equal(getServer(), d.getServer()) && equal(getPortNumber(), d.getPortNumber())
&& equal(getDatabaseName(), d.getDatabaseName())
&& equal(getSchema(), d.getSchema()) && equal(getUser(), d.getUser())
&& equal(getPassword(), d.getPassword());
&& equal(getPassword(), d.getPassword()) && equal(tablePrefix, d.tablePrefix);
}

@Override
public int hashCode() {
return Objects.hashCode(getServer(), getPortNumber(), getDatabaseName(), getSchema(),
getUser(), getPassword());
getUser(), getPassword(), tablePrefix);
}

String getDatabaseName() {
return databaseName;
}

@Nullable
String getUser() {
return user;
}

@Nullable
String getPassword() {
return password;
}
Expand All @@ -134,6 +185,11 @@ int getPortNumber() {
String getServer() {
return server;
}

@Nullable
String getTablePrefix() {
return tablePrefix;
}
}

final ConnectionConfig connectionConfig;
Expand All @@ -156,10 +212,14 @@ String getServer() {
*/
Environment(final String server, final int portNumber, final String databaseName,
final String schema, final String user, final String password,
final @Nullable String repositoryName, final @Nullable String tablePrefix) {
final @Nullable String repositoryName, @Nullable String tablePrefix) {

if (tablePrefix != null && tablePrefix.trim().isEmpty()) {
tablePrefix = null;
}

this.connectionConfig = new ConnectionConfig(server, portNumber, databaseName, schema, user,
password);
password, tablePrefix);
this.repositoryName = repositoryName;
this.tables = new TableNames(schema == null ? TableNames.DEFAULT_SCHEMA : schema,
tablePrefix == null ? TableNames.DEFAULT_TABLE_PREFIX : tablePrefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void init(URI repoUrl) {

int port = repoUrl.getPort();
if (-1 == port) {
port = 5432;
port = Environment.DEFAULT_DB_PORT;
}
portNumber = String.valueOf(port);

Expand Down Expand Up @@ -214,45 +214,20 @@ public static Properties getRootURIProperties(final URI rootRepoURI) {
*/
public static URI buildRepoURI(Properties props, String repoName) {
String server = props.getProperty(Environment.KEY_DB_SERVER);
String portNumber = props.getProperty(Environment.KEY_DB_PORT);
String portNumber = props.getProperty(Environment.KEY_DB_PORT,
String.valueOf(Environment.DEFAULT_DB_PORT));
String databaseName = props.getProperty(Environment.KEY_DB_NAME);
String schema = props.getProperty(Environment.KEY_DB_SCHEMA);
String userName = props.getProperty(Environment.KEY_DB_USERNAME);
String password = props.getProperty(Environment.KEY_DB_PASSWORD);
String tablePrefix = props.getProperty("tablePrefix");
if (tablePrefix != null && tablePrefix.trim().isEmpty()) {
tablePrefix = null;
}
// postgresql://<server>[:<port>]/database[/<schema>]/<repoid>?user=<username>&password=<pwd>
StringBuilder sb = new StringBuilder("postgresql://").append(server).append(":")
.append(portNumber).append("/").append(databaseName).append("/").append(schema)
.append("/").append(repoName);
StringBuilder args = new StringBuilder("");
if (userName != null) {
args.append("user=").append(userName);
}
if (password != null) {
if (args.length() > 0) {
args.append("&");
}
args.append("password=").append(password);
}
if (tablePrefix != null) {
if (args.length() > 0) {
args.append("&");
}
args.append("tablePrefix=").append(tablePrefix);
}
if (args.length() > 0) {
sb.append("?").append(args);
}

URI repoURI = null;
try {
repoURI = new URI(sb.toString());
} catch (URISyntaxException e) {
Throwables.propagate(e);
}
final int port = Integer.parseInt(portNumber);

Environment env = new Environment(server, port, databaseName, schema, userName, password,
repoName, tablePrefix);
final URI repoURI = env.connectionConfig.toURI();

return repoURI;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static String log(String sql, Logger log, Object... args) {
}

synchronized static DataSource newDataSource(Environment config) {
DataSource dataSource = DATASOURCE_POOL.acquire(config);
DataSource dataSource = DATASOURCE_POOL.acquire(config.connectionConfig);
return dataSource;
}

Expand Down

0 comments on commit a4b2dc2

Please sign in to comment.