Minor clean ups in DataSource Connection Pool related code

This commit is contained in:
Michael Vorburger
2020-03-28 17:58:39 +01:00
committed by Michael Vorburger ⛑️
parent 99794914a5
commit 5f2ba51f5a
4 changed files with 29 additions and 122 deletions
@@ -27,41 +27,41 @@ import org.springframework.stereotype.Service;
@Service
public class JDBCDriverConfig {
private final static String DRIVER_CLASS_PROPERTYNAME = "DRIVERCLASS_NAME" ;
private final static String PROTOCOL_PROPERTYNAME = "PROTOCOL" ;
private final static String SUBPROTOCOL_PROPERTYNAME = "SUB_PROTOCOL" ;
private final static String PORT_PROPERTYNAME = "PORT" ;
private final static String DRIVER_CLASS_PROPERTYNAME = "DRIVERCLASS_NAME";
private final static String PROTOCOL_PROPERTYNAME = "PROTOCOL";
private final static String SUBPROTOCOL_PROPERTYNAME = "SUB_PROTOCOL";
private final static String PORT_PROPERTYNAME = "PORT";
private String driverClassName ;
private String protocol ;
private String subProtocol ;
private Integer port ;
private String driverClassName;
private String protocol;
private String subProtocol;
private Integer port;
@Autowired ApplicationContext context ;
@Autowired ApplicationContext context;
@PostConstruct
protected void init() {
Environment environment = context.getEnvironment() ;
driverClassName = (String)environment.getProperty(DRIVER_CLASS_PROPERTYNAME) ;
protocol = (String) environment.getProperty(PROTOCOL_PROPERTYNAME) ;
subProtocol = (String) environment.getProperty(SUBPROTOCOL_PROPERTYNAME) ;
port = Integer.parseInt((String) environment.getProperty(PORT_PROPERTYNAME)) ;
driverClassName = environment.getProperty(DRIVER_CLASS_PROPERTYNAME) ;
protocol = environment.getProperty(PROTOCOL_PROPERTYNAME) ;
subProtocol = environment.getProperty(SUBPROTOCOL_PROPERTYNAME) ;
port = Integer.parseInt(environment.getProperty(PORT_PROPERTYNAME)) ;
}
public String getDriverClassName() {
return this.driverClassName ;
return this.driverClassName;
}
public String getProtocol() {
return this.protocol ;
return this.protocol;
}
public String getSubProtocol() {
return this.subProtocol ;
return this.subProtocol;
}
public Integer getPort() {
return this.port ;
return this.port;
}
public String constructProtocol(String schemaServer, String schemaServerPort, String schemaName) {
@@ -69,5 +69,4 @@ public class JDBCDriverConfig {
.append('/').append(schemaName).toString();
return url;
}
}
@@ -54,5 +54,4 @@ public class FineractPlatformTenant {
public FineractPlatformTenantConnection getConnection() {
return connection;
}
}
@@ -20,8 +20,7 @@ package org.apache.fineract.infrastructure.core.domain;
/**
* Holds DB server connection details.
*
* Holds Tenant's DB server connection connection details.
*/
public class FineractPlatformTenantConnection {
@@ -77,161 +76,78 @@ public class FineractPlatformTenantConnection {
this.testOnBorrow=tesOnBorrow;
}
//The Connection Protocol should be built based on jdbc.properties. We can't hard code this here and also, constructing protocol is not this class
//responsibility
/*public String databaseURL() {
final String url = new StringBuilder("jdbc:mysql:thin://").append(this.schemaServer).append(':').append(this.schemaServerPort)
.append('/').append(this.schemaName).toString();
return url;
}*/
/**
* @return the schemaServer
*/
public String getSchemaServer() {
return this.schemaServer;
}
/**
* @return the schemaServerPort
*/
public String getSchemaServerPort() {
return this.schemaServerPort;
}
/**
* @return the schemaUsername
*/
public String getSchemaUsername() {
return this.schemaUsername;
}
/**
* @return the schemaPassword
*/
public String getSchemaPassword() {
return this.schemaPassword;
}
/**
* @return the autoUpdateEnabled
*/
public boolean isAutoUpdateEnabled() {
return this.autoUpdateEnabled;
}
/**
* @return the initialSize
*/
public int getInitialSize() {
return this.initialSize;
}
/**
* @return the validationInterval
*/
public long getValidationInterval() {
return this.validationInterval;
}
/**
* @return the removeAbandoned
*/
public boolean isRemoveAbandoned() {
return this.removeAbandoned;
}
/**
* @return the removeAbandonedTimeout
*/
public int getRemoveAbandonedTimeout() {
return this.removeAbandonedTimeout;
}
/**
* @return the logAbandoned
*/
public boolean isLogAbandoned() {
return this.logAbandoned;
}
/**
* @return the abandonWhenPercentageFull
*/
public int getAbandonWhenPercentageFull() {
return this.abandonWhenPercentageFull;
}
/**
* @return the maxActive
*/
public int getMaxActive() {
return this.maxActive;
}
/**
* @return the minIdle
*/
public int getMinIdle() {
return this.minIdle;
}
/**
* @return the maxIdle
*/
public int getMaxIdle() {
return this.maxIdle;
}
/**
* @return the suspectTimeout
*/
public int getSuspectTimeout() {
return this.suspectTimeout;
}
/**
* @return the timeBetweenEvictionRunsMillis
*/
public int getTimeBetweenEvictionRunsMillis() {
return this.timeBetweenEvictionRunsMillis;
}
/**
* @return the minEvictableIdleTimeMillis
*/
public int getMinEvictableIdleTimeMillis() {
return this.minEvictableIdleTimeMillis;
}
/**
* @return the maxRetriesOnDeadlock
*/
public int getMaxRetriesOnDeadlock() {
return this.maxRetriesOnDeadlock;
}
/**
* @return the maxIntervalBetweenRetries
*/
public int getMaxIntervalBetweenRetries() {
return this.maxIntervalBetweenRetries;
}
@@ -249,6 +165,6 @@ public class FineractPlatformTenantConnection {
}
@Override
public String toString() {
return this.schemaName+":"+this.schemaServer+":"+this.schemaServerPort;
return this.schemaName + ":" + this.schemaServer + ":" + this.schemaServerPort;
}
}
@@ -31,7 +31,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
/**
* Implementation that returns a new or existing tomcat 7 jdbc connection pool
* Implementation that returns a new or existing connection pool
* datasource based on the tenant details stored in a {@link ThreadLocal}
* variable for this request.
*
@@ -45,7 +45,7 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
private final DataSource tenantDataSource;
@Autowired
private JDBCDriverConfig driverConfig ;
private JDBCDriverConfig driverConfig;
@Autowired
public TomcatJdbcDataSourcePerTenantService(final @Qualifier("hikariTenantDataSource") DataSource tenantDataSource) {
@@ -54,7 +54,6 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
@Override
public DataSource retrieveDataSource() {
// default to tenant database datasource
DataSource tenantDataSource = this.tenantDataSource;
@@ -63,12 +62,10 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
final FineractPlatformTenantConnection tenantConnection = tenant.getConnection();
synchronized (this.tenantToDataSourceMap) {
// if tenantConnection information available switch to
// appropriate
// datasource
// for that tenant.
if (this.tenantToDataSourceMap.containsKey(tenantConnection.getConnectionId())) {
tenantDataSource = this.tenantToDataSourceMap.get(tenantConnection.getConnectionId());
// if tenantConnection information available switch to the appropriate datasource for that tenant.
DataSource possibleDS = this.tenantToDataSourceMap.get(tenantConnection.getConnectionId());
if (possibleDS != null) {
tenantDataSource = possibleDS;
} else {
tenantDataSource = createNewDataSourceFor(tenantConnection);
this.tenantToDataSourceMap.put(tenantConnection.getConnectionId(), tenantDataSource);
@@ -79,13 +76,9 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
return tenantDataSource;
}
// creates the data source oltp and report databases
// creates the tenant data source for the oltp and report database
private DataSource createNewDataSourceFor(final FineractPlatformTenantConnection tenantConnectionObj) {
// see
// http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency
// see also org.apache.fineract.DataSourceProperties.setDefaults()
String jdbcUrl = this.driverConfig.constructProtocol(tenantConnectionObj.getSchemaServer(), tenantConnectionObj.getSchemaServerPort(), tenantConnectionObj.getSchemaName());
String jdbcUrl = this.driverConfig.constructProtocol(tenantConnectionObj.getSchemaServer(), tenantConnectionObj.getSchemaServerPort(), tenantConnectionObj.getSchemaName());
HikariConfig config = new HikariConfig();
config.setDriverClassName(this.driverConfig.getDriverClassName());
@@ -93,12 +86,12 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
config.setJdbcUrl(jdbcUrl);
config.setUsername(tenantConnectionObj.getSchemaUsername());
config.setPassword(tenantConnectionObj.getSchemaPassword());
config.setConnectionTestQuery("SELECT 1");
config.setValidationTimeout(tenantConnectionObj.getValidationInterval());
config.setAutoCommit(true);
// https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration
return new HikariDataSource(config);
}
}