Skip to content

Commit

Permalink
Add possibility to set maxActive in settings.yaml (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor authored Sep 12, 2024
1 parent ab40016 commit 79a005f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package cloudgene.mapred.database.util;

import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractDatabaseConnector implements DatabaseConnector {

protected static final Logger log = LoggerFactory.getLogger(DatabaseUpdater.class);

private int maxActive = 10;

private int maxWait = 10000;
Expand All @@ -26,6 +30,7 @@ protected BasicDataSource createDataSource() {
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

return dataSource;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ public static DatabaseConnector createConnector(Map<String, String> settings) {
String user = settings.get("user");
String password = settings.get("password");

return new MySqlConnector(host, port, database, user, password);
MySqlConnector connector = new MySqlConnector(host, port, database, user, password);
if (settings.containsKey("maxActive")) {
int maxActive = Integer.parseInt(settings.get("maxActive"));
connector.setMaxActive(maxActive);
}
if (settings.containsKey("maxWait")) {
int maxWait = Integer.parseInt(settings.get("maxWait"));
connector.setMaxWait(maxWait);
}
return connector;

} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,34 @@ public void connect() throws SQLException {

log.debug("Establishing connection to " + user + "@" + host + ":" + port);

if (DbUtils.loadDriver("com.mysql.jdbc.Driver")) {
try {
dataSource = createDataSource();

dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://" + host + "/" + database
+ "?autoReconnect=true&allowMultiQueries=true&rewriteBatchedStatements=true");
dataSource.setUsername(user);
dataSource.setPassword(password);

} catch (Exception e) {
e.printStackTrace();
}
if (DbUtils.loadDriver("com.mysql.cj.jdbc.Driver")) {

dataSource = createDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://" + host + "/" + database
+ "?autoReconnect=true&allowMultiQueries=true&rewriteBatchedStatements=true");
dataSource.setUsername(user);
dataSource.setPassword(password);

log.debug("Max Active Connections: " + dataSource.getMaxActive());
log.debug("Max Idle Connections: " + dataSource.getMaxIdle());
log.debug("Min Idle Connections: " + dataSource.getMinIdle());
log.debug("Initial Size: " + dataSource.getInitialSize());
log.debug("Max Wait: " + dataSource.getMaxWait());
log.debug("Default Auto-Commit: " + dataSource.getDefaultAutoCommit());
log.debug("Validation Query: " + dataSource.getValidationQuery());
log.debug("Test on Borrow: " + dataSource.getTestOnBorrow());
log.debug("Test on Return: " + dataSource.getTestOnReturn());

} else {
System.out.println("MySQL Driver class not found.");
throw new SQLException("MySQL Driver class not found.");
}

}

@Override
public void disconnect() throws SQLException {
dataSource.close();

}

@Override
Expand Down

0 comments on commit 79a005f

Please sign in to comment.