Skip to content

Commit

Permalink
Add JDBC interceptor configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Apr 26, 2017
1 parent e6fba4c commit 2be7116
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/manual/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,7 @@ validationInterval 30 seconds To avoid excess validat

validatorClassName none Name of a class of a custom validator implementation, which
will be used for validating connections.
jdbcInterceptors none A semicolon separated list of JDBC interceptor classnames.
============================ ===================== ===============================================================

.. _man-configuration-polymorphic:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,25 @@
* implementation, which will be used for validating connections.
* </td>
* </tr>
* <tr>
* <td>{@code jdbcInterceptors}</td>
* <td>(none)</td>
* <td>
* A semicolon separated list of classnames extending
* {@link org.apache.tomcat.jdbc.pool.JdbcInterceptor}
* </td>
* </tr>
* </table>
*/
public class DataSourceFactory implements PooledDataSourceFactory {
public Optional<String> getJdbcInterceptors() {
return jdbcInterceptors;
}

public void setJdbcInterceptors(Optional<String> jdbcInterceptors) {
this.jdbcInterceptors = jdbcInterceptors;
}

@SuppressWarnings("UnusedDeclaration")
public enum TransactionIsolation {
NONE(Connection.TRANSACTION_NONE),
Expand Down Expand Up @@ -400,6 +416,8 @@ public int get() {
@MinDuration(1)
private Duration removeAbandonedTimeout = Duration.seconds(60L);

private Optional<String> jdbcInterceptors = Optional.empty();

@JsonProperty
@Override
public boolean isAutoCommentsEnabled() {
Expand Down Expand Up @@ -859,7 +877,7 @@ public ManagedDataSource build(MetricRegistry metricRegistry, String name) {
poolConfig.setValidationQueryTimeout((int) validationQueryTimeout.toSeconds());
}
validatorClassName.ifPresent(poolConfig::setValidatorClassName);

jdbcInterceptors.ifPresent(poolConfig::setJdbcInterceptors);
return new ManagedPooledDataSource(poolConfig, metricRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void testFullConfiguration() throws Exception {
assertThat(ds.getCheckConnectionOnReturn()).isEqualTo(true);
assertThat(ds.getValidationQueryTimeout()).isEqualTo(Optional.of(Duration.seconds(3)));
assertThat(ds.getValidatorClassName()).isEqualTo(Optional.of("io.dropwizard.db.CustomConnectionValidator"));
assertThat(ds.getJdbcInterceptors()).isEqualTo(Optional.of("StatementFinalizer;SlowQueryReport"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.dropwizard.jackson.Jackson;
import io.dropwizard.util.Duration;
import io.dropwizard.validation.BaseValidator;
import org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
import org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -108,6 +110,16 @@ public void testCustomValidator() throws Exception {
assertThat(CustomConnectionValidator.loaded).isTrue();
}

@Test
public void testJdbcInterceptors() throws Exception {
factory.setJdbcInterceptors(Optional.of("StatementFinalizer;ConnectionState"));
final ManagedPooledDataSource source = (ManagedPooledDataSource) dataSource();

assertThat(source.getPoolProperties().getJdbcInterceptorsAsArray())
.extracting("interceptorClass")
.contains(StatementFinalizer.class, ConnectionState.class);
}

@Test
public void createDefaultFactory() throws Exception {
final DataSourceFactory factory = new YamlConfigurationFactory<>(DataSourceFactory.class,
Expand Down
1 change: 1 addition & 0 deletions dropwizard-db/src/test/resources/yaml/full_db_pool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ checkConnectionOnConnect: false
checkConnectionOnReturn: true

validatorClassName: io.dropwizard.db.CustomConnectionValidator
jdbcInterceptors: "StatementFinalizer;SlowQueryReport"

0 comments on commit 2be7116

Please sign in to comment.