Search in sources :

Example 1 with ProxyDataSourceBuilder

use of net.ttddyy.dsproxy.support.ProxyDataSourceBuilder in project dhis2-core by dhis2.

the class DataSourceConfig method dataSource.

@Bean("dataSource")
@DependsOn("actualDataSource")
@Primary
public DataSource dataSource(@Qualifier("actualDataSource") DataSource actualDataSource) {
    boolean enableQueryLogging = dhisConfig.isEnabled(ConfigurationKey.ENABLE_QUERY_LOGGING);
    if (!enableQueryLogging) {
        return actualDataSource;
    }
    PrettyQueryEntryCreator creator = new PrettyQueryEntryCreator();
    creator.setMultiline(true);
    SLF4JQueryLoggingListener listener = new SLF4JQueryLoggingListener();
    listener.setLogger("org.hisp.dhis.datasource.query");
    listener.setLogLevel(SLF4JLogLevel.INFO);
    listener.setQueryLogEntryCreator(creator);
    ProxyDataSourceBuilder b = ProxyDataSourceBuilder.create(actualDataSource).name("ProxyDS_DHIS2_" + dhisConfig.getProperty(ConfigurationKey.DB_POOL_TYPE) + "_" + CodeGenerator.generateCode(5)).logSlowQueryBySlf4j(Integer.parseInt(dhisConfig.getProperty(ConfigurationKey.SLOW_QUERY_LOGGING_THRESHOLD_TIME_MS)), TimeUnit.MILLISECONDS, SLF4JLogLevel.WARN).listener(listener).proxyResultSet();
    boolean elapsedTimeLogging = dhisConfig.isEnabled(ConfigurationKey.ELAPSED_TIME_QUERY_LOGGING_ENABLED);
    boolean methodLoggingEnabled = dhisConfig.isEnabled(ConfigurationKey.METHOD_QUERY_LOGGING_ENABLED);
    if (methodLoggingEnabled) {
        b.afterMethod(DataSourceConfig::executeAfterMethod);
    }
    if (elapsedTimeLogging) {
        b.afterQuery((execInfo, queryInfoList) -> log.info("Query took " + execInfo.getElapsedTime() + "msec"));
    }
    return b.build();
}
Also used : ProxyDataSourceBuilder(net.ttddyy.dsproxy.support.ProxyDataSourceBuilder) SLF4JQueryLoggingListener(net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener) DependsOn(org.springframework.context.annotation.DependsOn) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Aggregations

SLF4JQueryLoggingListener (net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener)1 ProxyDataSourceBuilder (net.ttddyy.dsproxy.support.ProxyDataSourceBuilder)1 Bean (org.springframework.context.annotation.Bean)1 DependsOn (org.springframework.context.annotation.DependsOn)1 Primary (org.springframework.context.annotation.Primary)1