net.ttddyy.dsproxy.support.ProxyDataSource Java Examples

The following examples show how to use net.ttddyy.dsproxy.support.ProxyDataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatasourceProxyBeanPostProcessor.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
    if (bean instanceof DataSource && !(bean instanceof ProxyDataSource)) {
        // Instead of directly returning a less specific datasource bean
        // (e.g.: HikariDataSource -> DataSource), return a proxy object.
        // See following links for why:
        //   https://stackoverflow.com/questions/44237787/how-to-use-user-defined-database-proxy-in-datajpatest
        //   https://gitter.im/spring-projects/spring-boot?at=5983602d2723db8d5e70a904
        //   http://blog.arnoldgalovics.com/2017/06/26/configuring-a-datasource-proxy-in-spring-boot/
        final ProxyFactory factory = new ProxyFactory(bean);
        factory.setProxyTargetClass(true);
        factory.addAdvice(new ProxyDataSourceInterceptor((DataSource) bean));
        return factory.getProxy();
    }
    return bean;
}
 
Example #2
Source File: DataSourceDecoratorAutoConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 6 votes vote down vote up
@Test
void testCustomDataSourceDecoratorApplied() {
    ApplicationContextRunner contextRunner = this.contextRunner.withUserConfiguration(TestDataSourceDecoratorConfiguration.class);

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        assertThat(dataSource).isNotNull();

        DataSource customDataSource = ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        assertThat(customDataSource).isInstanceOf(CustomDataSourceProxy.class);

        DataSource realDataSource = ((DecoratedDataSource) dataSource).getRealDataSource();
        assertThat(realDataSource).isInstanceOf(HikariDataSource.class);

        assertThatDataSourceDecoratingChain(dataSource).containsExactly(CustomDataSourceProxy.class, P6DataSource.class, ProxyDataSource.class,
                FlexyPoolDataSource.class);
    });
}
 
Example #3
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testRegisterLogAndSlowQueryLogUsingJUL() {
    ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("decorator.datasource.datasourceProxy.logging:jul");

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
        assertThat(chainListener.getListeners()).extracting("class").contains(JULSlowQueryListener.class);
        assertThat(chainListener.getListeners()).extracting("class").contains(JULQueryLoggingListener.class);
    });
}
 
Example #4
Source File: AbstractJPATest.java    From hibernate-master-class with Apache License 2.0 5 votes vote down vote up
private ProxyDataSource newDataSource() {
    JDBCDataSource actualDataSource = new JDBCDataSource();
    actualDataSource.setUrl("jdbc:hsqldb:mem:test");
    actualDataSource.setUser("sa");
    actualDataSource.setPassword("");
    ProxyDataSource proxyDataSource = new ProxyDataSource();
    proxyDataSource.setDataSource(actualDataSource);
    proxyDataSource.setListener(new SLF4JQueryLoggingListener());
    return proxyDataSource;
}
 
Example #5
Source File: TransactionIsolationExternalDataSourceExternalconfgiurationConnectionProviderTest.java    From hibernate-master-class with Apache License 2.0 5 votes vote down vote up
protected ProxyDataSource newDataSource() {
    JDBCDataSource actualDataSource = new JDBCDataSource();
    actualDataSource.setUrl("jdbc:hsqldb:mem:test");
    actualDataSource.setUser("sa");
    actualDataSource.setPassword("");
    Properties properties = new Properties();
    properties.setProperty("hsqldb.tx_level", "SERIALIZABLE");
    actualDataSource.setProperties(properties);
    ProxyDataSource proxyDataSource = new ProxyDataSource();
    proxyDataSource.setDataSource(actualDataSource);
    proxyDataSource.setListener(new SLF4JQueryLoggingListener());
    return proxyDataSource;
}
 
Example #6
Source File: ExternalDataSourceConnectionProviderTest.java    From hibernate-master-class with Apache License 2.0 5 votes vote down vote up
protected ProxyDataSource newDataSource() {
    JDBCDataSource actualDataSource = new JDBCDataSource();
    actualDataSource.setUrl("jdbc:hsqldb:mem:test");
    actualDataSource.setUser("sa");
    actualDataSource.setPassword("");
    ProxyDataSource proxyDataSource = new ProxyDataSource();
    proxyDataSource.setDataSource(actualDataSource);
    proxyDataSource.setListener(new SLF4JQueryLoggingListener());
    return proxyDataSource;
}
 
Example #7
Source File: DataSourceDecoratorAutoConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testDecoratingChainBuiltCorrectly() {
    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);

        DecoratedDataSource dataSource1 = context.getBean(DecoratedDataSource.class);
        assertThat(dataSource1).isNotNull();

        DataSource p6DataSource = dataSource1.getDecoratedDataSource();
        assertThat(p6DataSource).isNotNull();
        assertThat(p6DataSource).isInstanceOf(P6DataSource.class);

        DataSource proxyDataSource = (DataSource) new DirectFieldAccessor(p6DataSource)
                .getPropertyValue("realDataSource");
        assertThat(proxyDataSource).isNotNull();
        assertThat(proxyDataSource).isInstanceOf(ProxyDataSource.class);

        DataSource flexyDataSource = (DataSource) new DirectFieldAccessor(proxyDataSource)
                .getPropertyValue("dataSource");
        assertThat(flexyDataSource).isNotNull();
        assertThat(flexyDataSource).isInstanceOf(FlexyPoolDataSource.class);

        DataSource realDataSource = (DataSource) new DirectFieldAccessor(flexyDataSource)
                .getPropertyValue("targetDataSource");
        assertThat(realDataSource).isNotNull();
        assertThat(realDataSource).isInstanceOf(HikariDataSource.class);

        assertThatDataSourceDecoratingChain(dataSource).containsExactly(P6DataSource.class, ProxyDataSource.class, FlexyPoolDataSource.class);

        assertThat(dataSource.toString()).isEqualTo(
                "p6SpyDataSourceDecorator [com.p6spy.engine.spy.P6DataSource] -> " +
                        "proxyDataSourceDecorator [net.ttddyy.dsproxy.support.ProxyDataSource] -> " +
                        "flexyPoolDataSourceDecorator [com.vladmihalcea.flexypool.FlexyPoolDataSource] -> " +
                        "dataSource [com.zaxxer.hikari.HikariDataSource]");
    });
}
 
Example #8
Source File: DataSourceDecoratorAutoConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testDecoratingWhenDefaultProxyProviderNotAvailable() {
    ApplicationContextRunner contextRunner = this.contextRunner.withClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool"));

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);

        assertThat(((DecoratedDataSource) dataSource).getRealDataSource()).isInstanceOf(HikariDataSource.class);
        assertThatDataSourceDecoratingChain(dataSource).containsExactly(P6DataSource.class, ProxyDataSource.class);
    });
}
 
Example #9
Source File: DataSourceDecoratorAutoConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testDecoratingInDefaultOrder() {
    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);

        assertThatDataSourceDecoratingChain(dataSource).containsExactly(P6DataSource.class, ProxyDataSource.class, FlexyPoolDataSource.class);
    });
}
 
Example #10
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testCustomConnectionIdManager() {
    ApplicationContextRunner contextRunner = this.contextRunner.withUserConfiguration(CustomDataSourceProxyConfiguration.class);

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();

        assertThat(proxyDataSource.getConnectionIdManager()).isInstanceOf(DefaultConnectionIdManager.class);
    });
}
 
Example #11
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testGlobalConnectionIdManagerByDefault() {
    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();

        assertThat(proxyDataSource.getConnectionIdManager()).isInstanceOf(GlobalConnectionIdManager.class);
    });
}
 
Example #12
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testCustomListeners() {
    ApplicationContextRunner contextRunner = this.contextRunner.withUserConfiguration(CustomListenerConfiguration.class);

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        QueryExecutionListener queryExecutionListener = context.getBean(QueryExecutionListener.class);

        ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
        assertThat(chainListener.getListeners()).contains(queryExecutionListener);
    });
}
 
Example #13
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testCustomParameterAndQueryTransformer() {
    ApplicationContextRunner contextRunner = this.contextRunner.withUserConfiguration(CustomDataSourceProxyConfiguration.class);

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        ParameterTransformer parameterTransformer = context.getBean(ParameterTransformer.class);
        QueryTransformer queryTransformer = context.getBean(QueryTransformer.class);
        assertThat(proxyDataSource.getProxyConfig().getParameterTransformer()).isSameAs(parameterTransformer);
        assertThat(proxyDataSource.getProxyConfig().getQueryTransformer()).isSameAs(queryTransformer);
    });
}
 
Example #14
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testRegisterLogAndSlowQueryLogUsingApacheCommons() {
    ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("decorator.datasource.datasourceProxy.logging:commons");

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
        assertThat(chainListener.getListeners()).extracting("class").contains(CommonsSlowQueryListener.class);
        assertThat(chainListener.getListeners()).extracting("class").contains(CommonsQueryLoggingListener.class);
    });
}
 
Example #15
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testRegisterLogAndSlowQueryLogUsingSystemOut() {
    ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("decorator.datasource.datasource-proxy.logging:sysout");

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
        assertThat(chainListener.getListeners()).extracting("class").contains(SystemOutSlowQueryListener.class);
        assertThat(chainListener.getListeners()).extracting("class").contains(SystemOutQueryLoggingListener.class);
    });
}
 
Example #16
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testRegisterLogAndSlowQueryLogByUsingSlf4j() {
    ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("decorator.datasource.datasource-proxy.logging:slf4j");

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
        assertThat(chainListener.getListeners()).extracting("class").contains(SLF4JSlowQueryListener.class);
        assertThat(chainListener.getListeners()).extracting("class").contains(SLF4JQueryLoggingListener.class);
    });
}
 
Example #17
Source File: ProxyDataSourceConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testRegisterLogAndSlowQueryLogByDefaultToSlf4j() {
    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
        assertThat(chainListener.getListeners()).extracting("class").contains(SLF4JSlowQueryListener.class);
        assertThat(chainListener.getListeners()).extracting("class").contains(SLF4JQueryLoggingListener.class);
    });
}
 
Example #18
Source File: SleuthProxyDataSourceListenerAutoConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testDoesNotAddDatasourceProxyListenerIfNoTracer() {
    ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("spring.sleuth.enabled:false");

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
        assertThat(chainListener.getListeners()).extracting("class").doesNotContain(TracingQueryExecutionListener.class);
    });
}
 
Example #19
Source File: SleuthProxyDataSourceListenerAutoConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testAddsDatasourceProxyListener() {
    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource();
        ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
        assertThat(chainListener.getListeners()).extracting("class").contains(TracingQueryExecutionListener.class);
    });
}
 
Example #20
Source File: SqlTestBase.java    From quickperf with Apache License 2.0 5 votes vote down vote up
private PersistenceUnitInfo buildPersistenceUnitInfo() {
    DataSource baseDataSource = TestDataSourceBuilder.aDataSource().build();
    ProxyDataSource proxyDataSource = aDataSourceBuilder().buildProxy(baseDataSource);
    Properties hibernateProperties = getHibernateProperties();
    return aPersistenceUnitInfo().build(proxyDataSource
                                      , hibernateProperties
                                      , Book.class);
}
 
Example #21
Source File: SqlTestBaseJUnit4.java    From quickperf with Apache License 2.0 5 votes vote down vote up
private PersistenceUnitInfo buildPersistenceUnitInfo() {
    DataSource baseDataSource = TestDataSourceBuilder.aDataSource().build();
    ProxyDataSource proxyDataSource = aDataSourceBuilder().buildProxy(baseDataSource);
    Properties hibernateProperties = getHibernateProperties();
    return aPersistenceUnitInfo().build(proxyDataSource
                                      , hibernateProperties
                                      , Book.class);
}
 
Example #22
Source File: SqlTestBaseJUnit5.java    From quickperf with Apache License 2.0 5 votes vote down vote up
private PersistenceUnitInfo buildPersistenceUnitInfo() {
    DataSource baseDataSource = TestDataSourceBuilder.aDataSource().build();
    ProxyDataSource proxyDataSource = aDataSourceBuilder()
                                      .buildProxy(baseDataSource);
    Properties hibernateProperties = HibernateConfigBuilder.anHibernateConfig().build();
    return PersistenceUnitInfoBuilder.aPersistenceUnitInfo()
                                     .build(   proxyDataSource
                                             , hibernateProperties
                                             , Book.class);
}
 
Example #23
Source File: SqlTestBaseTestNG.java    From quickperf with Apache License 2.0 5 votes vote down vote up
private PersistenceUnitInfo buildPersistenceUnitInfo() {
    DataSource baseDataSource = TestDataSourceBuilder.aDataSource().build();
    ProxyDataSource proxyDataSource = aDataSourceBuilder().buildProxy(baseDataSource);
    Properties hibernateProperties = HibernateConfigBuilder.anHibernateConfig().build();
    return PersistenceUnitInfoBuilder.aPersistenceUnitInfo()
                                     .build( proxyDataSource
                                           , hibernateProperties
                                           , Book.class);
}
 
Example #24
Source File: QuickPerfProxyBeanPostProcessor.java    From quickperf with Apache License 2.0 4 votes vote down vote up
private boolean isProxyDataSourceBean(Object bean) {
    return bean.toString().contains(ProxyDataSource.class.getName());
}
 
Example #25
Source File: QuickPerfSqlDataSourceBuilder.java    From quickperf with Apache License 2.0 4 votes vote down vote up
public ProxyDataSource buildProxy(DataSource dataSource) {

        ProxyDataSource proxyDataSource = new ProxyDataSource();

        ChainListener chainListener = new ChainListener();

        DataSourceQuickPerfListener dataSourceQuickPerfListener = new DataSourceQuickPerfListener();
        chainListener.addListener(dataSourceQuickPerfListener);

        proxyDataSource.addListener(chainListener);

        proxyDataSource.setDataSource(dataSource);

        return proxyDataSource;

    }
 
Example #26
Source File: QuickPerfProxyBeanPostProcessor.java    From quickperf with Apache License 2.0 4 votes vote down vote up
private boolean isProxyDataSourceBean(Object bean) {
    return bean.toString().contains(ProxyDataSource.class.getName());
}
 
Example #27
Source File: QuickPerfProxyBeanPostProcessor.java    From quickperf with Apache License 2.0 4 votes vote down vote up
private boolean isProxyDataSourceBean(Object bean) {
    return bean.toString().contains(ProxyDataSource.class.getName());
}