Skip to content

Commit

Permalink
Remove obsolete code
Browse files Browse the repository at this point in the history
  • Loading branch information
sophokles73 committed Dec 16, 2023
1 parent b025a36 commit 7d51f37
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.agroal.api.security.SimplePassword;
import io.agroal.pool.DataSource;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.jdbc.JDBCClient;

/**
Expand All @@ -46,6 +45,7 @@ public class JdbcProperties {
public static final int DEFAULT_MAXIMUM_CONNECTION_TIME = 30;
public static final int DEFAULT_VALIDATION_TIME = 30;
public static final int DEFAULT_LEAK_TIME = 60;

private static final Logger log = LoggerFactory.getLogger(JdbcProperties.class);

private String url;
Expand Down Expand Up @@ -180,48 +180,12 @@ public void setTableName(final String tableName) {
* @param vertx The vertx instance to use.
* @param dataSourceProperties The properties.
* @return The client.
* @throws IllegalArgumentException if any of the properties are invalid.
*/
public static JDBCClient dataSource(final Vertx vertx, final JdbcProperties dataSourceProperties) {

final JsonObject config = new JsonObject()
.put("url", dataSourceProperties.getUrl())
.put("user", dataSourceProperties.getUsername());

// password is added later, after logging

if (dataSourceProperties.getDriverClass() != null) {
config.put("driver_class", dataSourceProperties.getDriverClass());
}

final String maxIdleLabel = "max_idle_time";
final String maxConnectionLabel = "max_connection_time";
final String validationLabel = "validation_time";
final String leakLabel = "leak_time";
final String minSizeLabel = "min_pool_size";
final String maxSizeLabel = "max_pool_size";
final String initSizeLabel = "initial_pool_size";

putValidValueIntoConfig(config, maxIdleLabel, dataSourceProperties.getMaximumIdleTime(), 0, true);
putValidValueIntoConfig(config, maxConnectionLabel, dataSourceProperties.getMaximumConnectionTime(), 0, true);
putValidValueIntoConfig(config, validationLabel, dataSourceProperties.getValidationTime(), 0, true);
putValidValueIntoConfig(config, leakLabel, dataSourceProperties.getLeakTime(), 0, true);
putValidValueIntoConfig(config, minSizeLabel, dataSourceProperties.getMinimumPoolSize(), 0, true);
putValidValueIntoConfig(config, maxSizeLabel, dataSourceProperties.getMaximumPoolSize(), Math.max(1, config.getInteger(minSizeLabel)), true);
// check that initial pool size is between min and max pool size
putValidValueIntoConfig(config, initSizeLabel, dataSourceProperties.getInitialPoolSize(), config.getInteger(minSizeLabel), true);
putValidValueIntoConfig(config, initSizeLabel, config.getInteger(initSizeLabel), config.getInteger(maxSizeLabel), false);

log.info("Creating new SQL client: {} - table: {}", config, dataSourceProperties.getTableName());
log.info("Creating new SQL client for table: {}", dataSourceProperties.getTableName());

// create new client

final int minSize = config.getInteger(minSizeLabel);
final int maxSize = config.getInteger(maxSizeLabel);
final int initSize = config.getInteger(initSizeLabel);
final Duration idleTime = Duration.ofSeconds(config.getInteger(maxIdleLabel));
final Duration connectionTime = Duration.ofSeconds(config.getInteger(maxConnectionLabel));
final Duration validationTime = Duration.ofSeconds(config.getInteger(validationLabel));
final Duration leakTime = Duration.ofSeconds(config.getInteger(leakLabel));
final NamePrincipal username = Optional
.ofNullable(dataSourceProperties.getUsername())
.map(NamePrincipal::new)
Expand All @@ -235,13 +199,13 @@ public static JDBCClient dataSource(final Vertx vertx, final JdbcProperties data
.metricsEnabled(false)
.dataSourceImplementation(DataSourceImplementation.AGROAL)
.connectionPoolConfiguration(poolConfig -> poolConfig
.minSize(minSize)
.maxSize(maxSize)
.initialSize(initSize)
.acquisitionTimeout(connectionTime)
.validationTimeout(validationTime)
.leakTimeout(leakTime)
.reapTimeout(idleTime)
.minSize(dataSourceProperties.getMinimumPoolSize())
.maxSize(dataSourceProperties.getMaximumPoolSize())
.initialSize(dataSourceProperties.getInitialPoolSize())
.acquisitionTimeout(Duration.ofSeconds(dataSourceProperties.getMaximumConnectionTime()))
.validationTimeout(Duration.ofSeconds(dataSourceProperties.getValidationTime()))
.leakTimeout(Duration.ofSeconds(dataSourceProperties.getLeakTime()))
.reapTimeout(Duration.ofSeconds(dataSourceProperties.getMaximumIdleTime()))
.connectionValidator(ConnectionValidator.defaultValidator())
.connectionFactoryConfiguration(connConfig -> connConfig
.jdbcUrl(dataSourceProperties.getUrl())
Expand All @@ -250,19 +214,5 @@ public static JDBCClient dataSource(final Vertx vertx, final JdbcProperties data
.credential(password)));

return JDBCClient.create(vertx, new DataSource(configuration.get()));

}

private static void putValidValueIntoConfig(final JsonObject config, final String label, final int value, final int limit, final boolean checkLowerLimit) {
if (checkLowerLimit && value < limit) {
log.warn("JDBC property {} has an illegal value. Value ({}) must not be smaller than {}. Value will be set to {}", label, value, limit, limit);
config.put(label, limit);
} else if (!checkLowerLimit && value > limit) {
log.warn("JDBC property {} has an illegal value. Value ({}) must not be bigger than {}. Value will be set to {}", label, value, limit, limit);
config.put(label, limit);
} else {
config.put(label, value);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.eclipse.hono.service.management.tenant.TenantManagementService;
import org.eclipse.hono.service.registration.RegistrationService;
import org.eclipse.hono.service.tenant.TenantService;
import org.h2.Driver;
import org.h2.tools.RunScript;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -165,7 +164,7 @@ private JdbcProperties resolveJdbcProperties() {
switch (DATABASE_TYPE) {
case H2:
final var dbFolderName = UUID.randomUUID().toString();
jdbc.setDriverClass(Driver.class.getName());
jdbc.setDriverClass(org.h2.Driver.class.getName());
jdbc.setUrl("jdbc:h2:" + BASE_DIR.resolve(dbFolderName).resolve("data").toAbsolutePath());
break;
case POSTGRESQL:
Expand Down

0 comments on commit 7d51f37

Please sign in to comment.