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 @Service
public class JDBCDriverConfig { public class JDBCDriverConfig {
private final static String DRIVER_CLASS_PROPERTYNAME = "DRIVERCLASS_NAME" ; private final static String DRIVER_CLASS_PROPERTYNAME = "DRIVERCLASS_NAME";
private final static String PROTOCOL_PROPERTYNAME = "PROTOCOL" ; private final static String PROTOCOL_PROPERTYNAME = "PROTOCOL";
private final static String SUBPROTOCOL_PROPERTYNAME = "SUB_PROTOCOL" ; private final static String SUBPROTOCOL_PROPERTYNAME = "SUB_PROTOCOL";
private final static String PORT_PROPERTYNAME = "PORT" ; private final static String PORT_PROPERTYNAME = "PORT";
private String driverClassName ; private String driverClassName;
private String protocol ; private String protocol;
private String subProtocol ; private String subProtocol;
private Integer port ; private Integer port;
@Autowired ApplicationContext context ; @Autowired ApplicationContext context;
@PostConstruct @PostConstruct
protected void init() { protected void init() {
Environment environment = context.getEnvironment() ; Environment environment = context.getEnvironment() ;
driverClassName = (String)environment.getProperty(DRIVER_CLASS_PROPERTYNAME) ; driverClassName = environment.getProperty(DRIVER_CLASS_PROPERTYNAME) ;
protocol = (String) environment.getProperty(PROTOCOL_PROPERTYNAME) ; protocol = environment.getProperty(PROTOCOL_PROPERTYNAME) ;
subProtocol = (String) environment.getProperty(SUBPROTOCOL_PROPERTYNAME) ; subProtocol = environment.getProperty(SUBPROTOCOL_PROPERTYNAME) ;
port = Integer.parseInt((String) environment.getProperty(PORT_PROPERTYNAME)) ; port = Integer.parseInt(environment.getProperty(PORT_PROPERTYNAME)) ;
} }
public String getDriverClassName() { public String getDriverClassName() {
return this.driverClassName ; return this.driverClassName;
} }
public String getProtocol() { public String getProtocol() {
return this.protocol ; return this.protocol;
} }
public String getSubProtocol() { public String getSubProtocol() {
return this.subProtocol ; return this.subProtocol;
} }
public Integer getPort() { public Integer getPort() {
return this.port ; return this.port;
} }
public String constructProtocol(String schemaServer, String schemaServerPort, String schemaName) { public String constructProtocol(String schemaServer, String schemaServerPort, String schemaName) {
@@ -69,5 +69,4 @@ public class JDBCDriverConfig {
.append('/').append(schemaName).toString(); .append('/').append(schemaName).toString();
return url; return url;
} }
} }
@@ -54,5 +54,4 @@ public class FineractPlatformTenant {
public FineractPlatformTenantConnection getConnection() { public FineractPlatformTenantConnection getConnection() {
return connection; 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 { public class FineractPlatformTenantConnection {
@@ -77,161 +76,78 @@ public class FineractPlatformTenantConnection {
this.testOnBorrow=tesOnBorrow; 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() { public String getSchemaServer() {
return this.schemaServer; return this.schemaServer;
} }
/**
* @return the schemaServerPort
*/
public String getSchemaServerPort() { public String getSchemaServerPort() {
return this.schemaServerPort; return this.schemaServerPort;
} }
/**
* @return the schemaUsername
*/
public String getSchemaUsername() { public String getSchemaUsername() {
return this.schemaUsername; return this.schemaUsername;
} }
/**
* @return the schemaPassword
*/
public String getSchemaPassword() { public String getSchemaPassword() {
return this.schemaPassword; return this.schemaPassword;
} }
/**
* @return the autoUpdateEnabled
*/
public boolean isAutoUpdateEnabled() { public boolean isAutoUpdateEnabled() {
return this.autoUpdateEnabled; return this.autoUpdateEnabled;
} }
/**
* @return the initialSize
*/
public int getInitialSize() { public int getInitialSize() {
return this.initialSize; return this.initialSize;
} }
/**
* @return the validationInterval
*/
public long getValidationInterval() { public long getValidationInterval() {
return this.validationInterval; return this.validationInterval;
} }
/**
* @return the removeAbandoned
*/
public boolean isRemoveAbandoned() { public boolean isRemoveAbandoned() {
return this.removeAbandoned; return this.removeAbandoned;
} }
/**
* @return the removeAbandonedTimeout
*/
public int getRemoveAbandonedTimeout() { public int getRemoveAbandonedTimeout() {
return this.removeAbandonedTimeout; return this.removeAbandonedTimeout;
} }
/**
* @return the logAbandoned
*/
public boolean isLogAbandoned() { public boolean isLogAbandoned() {
return this.logAbandoned; return this.logAbandoned;
} }
/**
* @return the abandonWhenPercentageFull
*/
public int getAbandonWhenPercentageFull() { public int getAbandonWhenPercentageFull() {
return this.abandonWhenPercentageFull; return this.abandonWhenPercentageFull;
} }
/**
* @return the maxActive
*/
public int getMaxActive() { public int getMaxActive() {
return this.maxActive; return this.maxActive;
} }
/**
* @return the minIdle
*/
public int getMinIdle() { public int getMinIdle() {
return this.minIdle; return this.minIdle;
} }
/**
* @return the maxIdle
*/
public int getMaxIdle() { public int getMaxIdle() {
return this.maxIdle; return this.maxIdle;
} }
/**
* @return the suspectTimeout
*/
public int getSuspectTimeout() { public int getSuspectTimeout() {
return this.suspectTimeout; return this.suspectTimeout;
} }
/**
* @return the timeBetweenEvictionRunsMillis
*/
public int getTimeBetweenEvictionRunsMillis() { public int getTimeBetweenEvictionRunsMillis() {
return this.timeBetweenEvictionRunsMillis; return this.timeBetweenEvictionRunsMillis;
} }
/**
* @return the minEvictableIdleTimeMillis
*/
public int getMinEvictableIdleTimeMillis() { public int getMinEvictableIdleTimeMillis() {
return this.minEvictableIdleTimeMillis; return this.minEvictableIdleTimeMillis;
} }
/**
* @return the maxRetriesOnDeadlock
*/
public int getMaxRetriesOnDeadlock() { public int getMaxRetriesOnDeadlock() {
return this.maxRetriesOnDeadlock; return this.maxRetriesOnDeadlock;
} }
/**
* @return the maxIntervalBetweenRetries
*/
public int getMaxIntervalBetweenRetries() { public int getMaxIntervalBetweenRetries() {
return this.maxIntervalBetweenRetries; return this.maxIntervalBetweenRetries;
} }
@@ -249,6 +165,6 @@ public class FineractPlatformTenantConnection {
} }
@Override @Override
public String toString() { 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; 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} * datasource based on the tenant details stored in a {@link ThreadLocal}
* variable for this request. * variable for this request.
* *
@@ -45,7 +45,7 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
private final DataSource tenantDataSource; private final DataSource tenantDataSource;
@Autowired @Autowired
private JDBCDriverConfig driverConfig ; private JDBCDriverConfig driverConfig;
@Autowired @Autowired
public TomcatJdbcDataSourcePerTenantService(final @Qualifier("hikariTenantDataSource") DataSource tenantDataSource) { public TomcatJdbcDataSourcePerTenantService(final @Qualifier("hikariTenantDataSource") DataSource tenantDataSource) {
@@ -54,7 +54,6 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
@Override @Override
public DataSource retrieveDataSource() { public DataSource retrieveDataSource() {
// default to tenant database datasource // default to tenant database datasource
DataSource tenantDataSource = this.tenantDataSource; DataSource tenantDataSource = this.tenantDataSource;
@@ -63,12 +62,10 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
final FineractPlatformTenantConnection tenantConnection = tenant.getConnection(); final FineractPlatformTenantConnection tenantConnection = tenant.getConnection();
synchronized (this.tenantToDataSourceMap) { synchronized (this.tenantToDataSourceMap) {
// if tenantConnection information available switch to // if tenantConnection information available switch to the appropriate datasource for that tenant.
// appropriate DataSource possibleDS = this.tenantToDataSourceMap.get(tenantConnection.getConnectionId());
// datasource if (possibleDS != null) {
// for that tenant. tenantDataSource = possibleDS;
if (this.tenantToDataSourceMap.containsKey(tenantConnection.getConnectionId())) {
tenantDataSource = this.tenantToDataSourceMap.get(tenantConnection.getConnectionId());
} else { } else {
tenantDataSource = createNewDataSourceFor(tenantConnection); tenantDataSource = createNewDataSourceFor(tenantConnection);
this.tenantToDataSourceMap.put(tenantConnection.getConnectionId(), tenantDataSource); this.tenantToDataSourceMap.put(tenantConnection.getConnectionId(), tenantDataSource);
@@ -79,13 +76,9 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
return tenantDataSource; 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) { private DataSource createNewDataSourceFor(final FineractPlatformTenantConnection tenantConnectionObj) {
// see String jdbcUrl = this.driverConfig.constructProtocol(tenantConnectionObj.getSchemaServer(), tenantConnectionObj.getSchemaServerPort(), tenantConnectionObj.getSchemaName());
// 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());
HikariConfig config = new HikariConfig(); HikariConfig config = new HikariConfig();
config.setDriverClassName(this.driverConfig.getDriverClassName()); config.setDriverClassName(this.driverConfig.getDriverClassName());
@@ -93,12 +86,12 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
config.setJdbcUrl(jdbcUrl); config.setJdbcUrl(jdbcUrl);
config.setUsername(tenantConnectionObj.getSchemaUsername()); config.setUsername(tenantConnectionObj.getSchemaUsername());
config.setPassword(tenantConnectionObj.getSchemaPassword()); config.setPassword(tenantConnectionObj.getSchemaPassword());
config.setConnectionTestQuery("SELECT 1"); config.setConnectionTestQuery("SELECT 1");
config.setValidationTimeout(tenantConnectionObj.getValidationInterval()); config.setValidationTimeout(tenantConnectionObj.getValidationInterval());
config.setAutoCommit(true); config.setAutoCommit(true);
// https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration
return new HikariDataSource(config); return new HikariDataSource(config);
} }
} }