Skip to content

Commit

Permalink
[#3542] Add connection pool params to JDBC configuration
Browse files Browse the repository at this point in the history
Added minimumPoolSize, initialPoolSize and maximumIdleTime as parameters to the JDBC configuration
  • Loading branch information
mattkaem authored Nov 4, 2023
1 parent d6b2f25 commit a2def04
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
package org.eclipse.hono.service.base.jdbc.config;

import java.util.Optional;
import java.util.OptionalInt;

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.ConfigMapping.NamingStrategy;
import io.smallrye.config.WithDefault;

/**
* Configuration properties for a JDBC service.
Expand Down Expand Up @@ -58,7 +58,32 @@ public interface JdbcOptions {
*
* @return The maximum number of connections in the pool.
*/
OptionalInt maximumPoolSize();
@WithDefault("15")
int maximumPoolSize();

/**
* Gets the minimum size of the DB connection pool.
*
* @return The minimum number of connections in the pool.
*/
@WithDefault("3")
int minimumPoolSize();

/**
* Gets the initial size of the DB connection pool.
*
* @return The initial number of connections in the pool.
*/
@WithDefault("3")
int initialPoolSize();

/**
* Gets the maximum idle time of connections in the DB connection pool.
*
* @return The maximum idle time of connections in the pool.
*/
@WithDefault("3600")
int maximumIdleTime();

/**
* Gets the name of the table that contains the data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@
@JsonInclude(value = Include.NON_NULL)
public class JdbcProperties {

public static final int DEFAULT_MAXIMUM_POOL_SIZE = 15;
public static final int DEFAULT_MINIMUM_POOL_SIZE = 3;
public static final int DEFAULT_INITIAL_POOL_SIZE = 3;
public static final int DEFAULT_MAXIMUM_IDLE_TIME = 3600;
private static final Logger log = LoggerFactory.getLogger(JdbcProperties.class);

private String url;
private String driverClass;
private String username;
private String password;
private Integer maximumPoolSize;
private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE;
private int minimumPoolSize = DEFAULT_MINIMUM_POOL_SIZE;
private int initialPoolSize = DEFAULT_INITIAL_POOL_SIZE;
private int maximumIdleTime = DEFAULT_MAXIMUM_IDLE_TIME;
private String tableName;

/**
Expand All @@ -56,7 +63,10 @@ public JdbcProperties() {
public JdbcProperties(final JdbcOptions options) {
Objects.requireNonNull(options);
setDriverClass(options.driverClass());
options.maximumPoolSize().ifPresent(this::setMaximumPoolSize);
setMaximumPoolSize(options.maximumPoolSize());
setMinimumPoolSize(options.minimumPoolSize());
setInitialPoolSize(options.initialPoolSize());
setMaximumIdleTime(options.maximumIdleTime());
options.password().ifPresent(this::setPassword);
options.tableName().ifPresent(this::setTableName);
setUrl(options.url());
Expand Down Expand Up @@ -91,13 +101,34 @@ public String getPassword() {
return password;
}

public void setMaximumPoolSize(final Integer maximumPoolSize) {
public void setMaximumPoolSize(final int maximumPoolSize) {
this.maximumPoolSize = maximumPoolSize;
}
public Integer getMaximumPoolSize() {
public int getMaximumPoolSize() {
return maximumPoolSize;
}

public void setMinimumPoolSize(final int minimumPoolSize) {
this.minimumPoolSize = minimumPoolSize;
}
public int getMinimumPoolSize() {
return minimumPoolSize;
}

public void setInitialPoolSize(final int initialPoolSize) {
this.initialPoolSize = initialPoolSize;
}
public int getInitialPoolSize() {
return initialPoolSize;
}

public void setMaximumIdleTime(final int maximumIdleTime) {
this.maximumIdleTime = maximumIdleTime;
}
public int getMaximumIdleTime() {
return maximumIdleTime;
}

public String getTableName() {
return tableName;
}
Expand All @@ -123,9 +154,17 @@ public static JDBCClient dataSource(final Vertx vertx, final JdbcProperties data
if (dataSourceProperties.getDriverClass() != null) {
config.put("driver_class", dataSourceProperties.getDriverClass());
}
if (dataSourceProperties.getMaximumPoolSize() != null) {
config.put("max_pool_size", dataSourceProperties.getMaximumPoolSize());
}

final String minSizeLabel = "min_pool_size";
final String maxSizeLabel = "max_pool_size";
final String initSizeLabel = "initial_pool_size";

putValidValueIntoConfig(config, "max_idle_time", dataSourceProperties.getMaximumIdleTime(), 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());

Expand All @@ -140,4 +179,16 @@ public static JDBCClient dataSource(final Vertx vertx, final JdbcProperties data

}

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 @@ -65,13 +65,19 @@ and availability.
| `HONO_REGISTRY_JDBC_ADAPTER_DRIVERCLASS` <br> `hono.registry.jdbc.adapter.driverClass` | no | The default driver registered for the JDBC URL. | The class name of the JDBC driver.|
| `HONO_REGISTRY_JDBC_ADAPTER_USERNAME` <br> `hono.registry.jdbc.adapter.username` | no | - | The username used to access the database. |
| `HONO_REGISTRY_JDBC_ADAPTER_PASSWORD` <br> `hono.registry.jdbc.adapter.password` | no | - | The password used to access the database. |
| `HONO_REGISTRY_JDBC_ADAPTER_MAXIMUMPOOLSIZE` <br> `hono.registry.jdbc.adapter.maximumPoolSize` | no | Depends on the connection pool implementation. `15` for C3P0. | The maximum size of the connection pool. |
| `HONO_REGISTRY_JDBC_ADAPTER_MAXIMUMPOOLSIZE` <br> `hono.registry.jdbc.adapter.maximumPoolSize` | no | `15` | The maximum size of the connection pool. |
| `HONO_REGISTRY_JDBC_ADAPTER_MINIMUMPOOLSIZE` <br> `hono.registry.jdbc.adapter.minimumPoolSize` | no | `3` | The minimum size of the connection pool. |
| `HONO_REGISTRY_JDBC_ADAPTER_INITIALPOOLSIZE` <br> `hono.registry.jdbc.adapter.initialPoolSize` | no | `3` | Number of connections a pool will try to acquire upon startup. Should be between minPoolSize and maxPoolSize. |
| `HONO_REGISTRY_JDBC_ADAPTER_MAXIMUMIDLETIME` <br> `hono.registry.jdbc.adapter.maximumIdleTime` | no | `3600` | Seconds a connection can remain pooled but unused before being discarded. Zero means idle connections never expire. |
| `HONO_REGISTRY_JDBC_ADAPTER_TABLENAME` <br> `hono.registry.jdbc.adapter.tableName` | no | - | The name of the table the datastore uses. If the datastore requires multiple tables, this is the prefix. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_URL` <br> `hono.registry.jdbc.management.url` | yes | - | The JDBC URL to the database. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_DRIVERCLASS` <br> `hono.registry.jdbc.management.driverClass` | no | The default driver registered for the JDBC URL. | The class name of the JDBC driver. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_USERNAME` <br> `hono.registry.jdbc.management.username` | no | - | The username used to access the database. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_PASSWORD` <br> `hono.registry.jdbc.management.password` | no | - | The password used to access the database. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_MAXIMUMPOOLSIZE` <br> `hono.registry.jdbc.management.maximumPoolSize` | no | Depends on the connection pool implementation. `15` for C3P0. | The maximum size of the connection pool. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_MAXIMUMPOOLSIZE` <br> `hono.registry.jdbc.management.maximumPoolSize` | no | `15` | The maximum size of the connection pool. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_MINIMUMPOOLSIZE` <br> `hono.registry.jdbc.management.minimumPoolSize` | no | `3` | The minimum size of the connection pool. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_INITIALPOOLSIZE` <br> `hono.registry.jdbc.management.initialPoolSize` | no | `3` | Number of connections a pool will try to acquire upon startup. Should be between minPoolSize and maxPoolSize. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_MAXIMUMIDLETIME` <br> `hono.registry.jdbc.management.maximumIdleTime` | no | `3600` | Seconds a connection can remain pooled but unused before being discarded. Zero means idle connections never expire. |
| `HONO_REGISTRY_JDBC_MANAGEMENT_TABLENAME` <br> `hono.registry.jdbc.management.tableName` | no | - | The name of the table the datastore uses. If the datastore requires multiple tables, this is the prefix. |
| `HONO_REGISTRY_SVC_CREDENTIALSTTL` <br> `hono.registry.svc.credentialsTtl` | no | `1m` | The TTL for credentials responses. |
| `HONO_REGISTRY_SVC_HASHALGORITHMSWHITELIST` <br> `hono.registry.svc.hashAlgorithmsWhitelist` | no | `empty` | An array of supported hashing algorithms to be used with the `hashed-password` type of credentials. When not set, all values will be accepted. |
Expand All @@ -83,13 +89,19 @@ and availability.
| `HONO_TENANT_JDBC_ADAPTER_DRIVERCLASS` <br> `hono.tenant.jdbc.adapter.driverClass` | no | The default driver registered for the JDBC URL. | The class name of the JDBC driver.|
| `HONO_TENANT_JDBC_ADAPTER_USERNAME` <br> `hono.tenant.jdbc.adapter.username` | no | - | The username used to access the database. |
| `HONO_TENANT_JDBC_ADAPTER_PASSWORD` <br> `hono.tenant.jdbc.adapter.password` | no | - | The password used to access the database. |
| `HONO_TENANT_JDBC_ADAPTER_MAXIMUMPOOLSIZE` <br> `hono.tenant.jdbc.adapter.maximumPoolSize` | no | Depends on the connection pool implementation. `15` for C3P0. | The maximum size of the connection pool. |
| `HONO_TENANT_JDBC_ADAPTER_MAXIMUMPOOLSIZE` <br> `hono.tenant.jdbc.adapter.maximumPoolSize` | no | `15` | The maximum size of the connection pool. |
| `HONO_TENANT_JDBC_ADAPTER_MINIMUMPOOLSIZE` <br> `hono.tenant.jdbc.adapter.minimumPoolSize` | no | `3` | The minimum size of the connection pool. |
| `HONO_TENANT_JDBC_ADAPTER_INITIALPOOLSIZE` <br> `hono.tenant.jdbc.adapter.initialPoolSize` | no | `3` | Number of connections a pool will try to acquire upon startup. Should be between minPoolSize and maxPoolSize. |
| `HONO_TENANT_JDBC_ADAPTER_MAXIMUMIDLETIME` <br> `hono.tenant.jdbc.adapter.maximumIdleTime` | no | `3600` | Seconds a connection can remain pooled but unused before being discarded. Zero means idle connections never expire. |
| `HONO_TENANT_JDBC_ADAPTER_TABLENAME` <br> `hono.tenant.jdbc.adapter.tableName` | no | - | The name of the table the datastore uses. If the datastore requires multiple tables, this is the prefix. |
| `HONO_TENANT_JDBC_MANAGEMENT_URL` <br> `hono.tenant.jdbc.management.url` | yes | - | The JDBC URL to the database. |
| `HONO_TENANT_JDBC_MANAGEMENT_DRIVERCLASS` <br> `hono.tenant.jdbc.management.driverClass` | no | The default driver registered for the JDBC URL. | The class name of the JDBC driver. |
| `HONO_TENANT_JDBC_MANAGEMENT_USERNAME` <br> `hono.tenant.jdbc.management.username` | no | - | The username used to access the database. |
| `HONO_TENANT_JDBC_MANAGEMENT_PASSWORD` <br> `hono.tenant.jdbc.management.password` | no | - | The password used to access the database. |
| `HONO_TENANT_JDBC_MANAGEMENT_MAXIMUMPOOLSIZE` <br> `hono.tenant.jdbc.management.maximumPoolSize` | no | Depends on the connection pool implementation. `15` for C3P0. | The maximum size of the connection pool. |
| `HONO_TENANT_JDBC_MANAGEMENT_MAXIMUMPOOLSIZE` <br> `hono.tenant.jdbc.management.maximumPoolSize` | no | `15` | The maximum size of the connection pool. |
| `HONO_TENANT_JDBC_MANAGEMENT_MINIMUMPOOLSIZE` <br> `hono.tenant.jdbc.management.minimumPoolSize` | no | `3` | The minimum size of the connection pool. |
| `HONO_TENANT_JDBC_MANAGEMENT_INITIALPOOLSIZE` <br> `hono.tenant.jdbc.management.initialPoolSize` | no | `3` | Number of connections a pool will try to acquire upon startup. Should be between minPoolSize and maxPoolSize. |
| `HONO_TENANT_JDBC_MANAGEMENT_MAXIMUMIDLETIME` <br> `hono.tenant.jdbc.management.maximumIdleTime` | no | `3600` | Seconds a connection can remain pooled but unused before being discarded. Zero means idle connections never expire. |
| `HONO_TENANT_JDBC_MANAGEMENT_TABLENAME` <br> `hono.tenant.jdbc.management.tableName` | no | - | The name of the table the datastore uses. If the datastore requires multiple tables, this is the prefix. |
| `HONO_TENANT_SVC_TENANTTTL` <br> `hono.tenant.service.tenantTtl` | no | `1m` | The TTL for tenant responses. |

Expand Down

0 comments on commit a2def04

Please sign in to comment.