[New] added connection pool properties for PersistenceHandler

- e.g. for PostgreSQL enforce pool size:
  <db.pool.maximumPoolSize>1</db.pool.maximumPoolSize>
This commit is contained in:
Robert von Burg 2015-04-30 08:46:18 +02:00
parent a6b320268f
commit 01535fb515
7 changed files with 49 additions and 13 deletions

View File

@ -32,12 +32,15 @@ import javax.sql.DataSource;
import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchRealm; import li.strolch.agent.api.StrolchRealm;
import li.strolch.persistence.api.StrolchPersistenceException; import li.strolch.persistence.api.StrolchPersistenceException;
import li.strolch.runtime.StrolchConstants;
/** /**
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
*/ */
public abstract class DbConnectionBuilder { public abstract class DbConnectionBuilder {
private static final String PROP_DB_POOL_PREFIX = "db.pool";
private ComponentContainer container; private ComponentContainer container;
private ComponentConfiguration configuration; private ComponentConfiguration configuration;
@ -69,7 +72,37 @@ public abstract class DbConnectionBuilder {
String username = configuration.getString(dbUsernameKey, null); String username = configuration.getString(dbUsernameKey, null);
String password = configuration.getString(dbPasswordKey, null); String password = configuration.getString(dbPasswordKey, null);
DataSource ds = build(realmName, dbUrl, username, password, null); // find any pool configuration values
Set<String> propertyKeys = configuration.getPropertyKeys();
Properties props = new Properties();
for (String key : propertyKeys) {
if (!key.startsWith(PROP_DB_POOL_PREFIX))
continue;
// TODO we should change how properties for realms are configured
// since defaultRealm does not have to be on the key, we need this hack:
String[] segments = key.split("\\.");
String poolKey;
String foundRealm;
if (segments.length == 4) {
// ends with realm
foundRealm = segments[3];
} else if (segments.length == 3) {
// default realm
foundRealm = StrolchConstants.DEFAULT_REALM;
} else {
throw new IllegalArgumentException("Can't detect realm of this property: " + key);
}
// see if this is our realm
if (!foundRealm.equals(realmName))
continue;
poolKey = segments[2];
String value = configuration.getString(key, null);
props.setProperty(poolKey, value);
}
DataSource ds = build(realmName, dbUrl, username, password, props);
dsMap.put(realmName, ds); dsMap.put(realmName, ds);
} }

View File

@ -43,6 +43,7 @@
<db.url>jdbc:postgresql://localhost/testdb</db.url> <db.url>jdbc:postgresql://localhost/testdb</db.url>
<db.username>testuser</db.username> <db.username>testuser</db.username>
<db.password>test</db.password> <db.password>test</db.password>
<db.pool.maximumPoolSize>1</db.pool.maximumPoolSize>
</Properties> </Properties>
</Component> </Component>
<Component> <Component>

View File

@ -57,19 +57,15 @@ public final class PostgreSqlDbConnectionBuilder extends DbConnectionBuilder {
public DataSource build(String realm, String url, String username, String password, Properties props) { public DataSource build(String realm, String url, String username, String password, Properties props) {
HikariDataSource ds; HikariDataSource ds;
if (props != null) { HikariConfig config = new HikariConfig(props);
props.setProperty("dataSource.autoCommit", "false"); config.setAutoCommit(false);
HikariConfig config = new HikariConfig(props); config.setPoolName(realm);
ds = new HikariDataSource(config); config.setJdbcUrl(url);
} else { config.setUsername(username);
ds = new HikariDataSource(); config.setPassword(password);
ds.setAutoCommit(false);
}
ds.setPoolName(realm); ds = new HikariDataSource(config);
ds.setJdbcUrl(url); System.err.println(ds.getMaximumPoolSize());
ds.setUsername(username);
ds.setPassword(password);
return new StrolchPostgreDataSource(ds); return new StrolchPostgreDataSource(ds);
} }

View File

@ -37,6 +37,7 @@
<db.url>jdbc:postgresql://localhost/testdb</db.url> <db.url>jdbc:postgresql://localhost/testdb</db.url>
<db.username>testuser</db.username> <db.username>testuser</db.username>
<db.password>test</db.password> <db.password>test</db.password>
<db.pool.maximumPoolSize>1</db.pool.maximumPoolSize>
</Properties> </Properties>
</Component> </Component>
</env> </env>

View File

@ -41,10 +41,12 @@
<db.url.first>jdbc:postgresql://localhost/testdb1</db.url.first> <db.url.first>jdbc:postgresql://localhost/testdb1</db.url.first>
<db.username.first>testuser1</db.username.first> <db.username.first>testuser1</db.username.first>
<db.password.first>test</db.password.first> <db.password.first>test</db.password.first>
<db.pool.maximumPoolSize.first>1</db.pool.maximumPoolSize.first>
<db.url.second>jdbc:postgresql://localhost/testdb2</db.url.second> <db.url.second>jdbc:postgresql://localhost/testdb2</db.url.second>
<db.username.second>testuser2</db.username.second> <db.username.second>testuser2</db.username.second>
<db.password.second>test</db.password.second> <db.password.second>test</db.password.second>
<db.pool.maximumPoolSize.second>1</db.pool.maximumPoolSize.second>
</Properties> </Properties>
</Component> </Component>
</env> </env>

View File

@ -36,6 +36,7 @@
<db.url>jdbc:postgresql://localhost/testdb</db.url> <db.url>jdbc:postgresql://localhost/testdb</db.url>
<db.username>testuser</db.username> <db.username>testuser</db.username>
<db.password>test</db.password> <db.password>test</db.password>
<db.pool.maximumPoolSize>1</db.pool.maximumPoolSize>
</Properties> </Properties>
</Component> </Component>
</env> </env>

View File

@ -52,10 +52,12 @@
<db.url.svcCached>jdbc:postgresql://localhost/cacheduserdb</db.url.svcCached> <db.url.svcCached>jdbc:postgresql://localhost/cacheduserdb</db.url.svcCached>
<db.username.svcCached>cacheduser</db.username.svcCached> <db.username.svcCached>cacheduser</db.username.svcCached>
<db.password.svcCached>test</db.password.svcCached> <db.password.svcCached>test</db.password.svcCached>
<db.pool.maximumPoolSize.svcCached>1</db.pool.maximumPoolSize.svcCached>
<db.url.svcTransactional>jdbc:postgresql://localhost/transactionaluserdb</db.url.svcTransactional> <db.url.svcTransactional>jdbc:postgresql://localhost/transactionaluserdb</db.url.svcTransactional>
<db.username.svcTransactional>transactionaluser</db.username.svcTransactional> <db.username.svcTransactional>transactionaluser</db.username.svcTransactional>
<db.password.svcTransactional>test</db.password.svcTransactional> <db.password.svcTransactional>test</db.password.svcTransactional>
<db.pool.maximumPoolSize.svcTransactional>1</db.pool.maximumPoolSize.svcTransactional>
</Properties> </Properties>
</Component> </Component>