com.vladmihalcea.flexypool.FlexyPoolDataSource Java Examples

The following examples show how to use com.vladmihalcea.flexypool.FlexyPoolDataSource. 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: 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 #2
Source File: FlexyPoolTestConfiguration.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource actualDataSource() {
    final HikariDataSource dataSource = (HikariDataSource) super.actualDataSource();

    com.vladmihalcea.flexypool.config.Configuration<HikariDataSource> configuration =
        new com.vladmihalcea.flexypool.config.Configuration.Builder<>(
            "flexy-pool-test",
            dataSource,
            HikariCPPoolAdapter.FACTORY
        )
    .build();

    return new FlexyPoolDataSource<>(
        configuration
    );
}
 
Example #3
Source File: JTAFlexyPoolTestConfiguration.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource actualDataSource() {
    final PoolingDataSource poolingDataSource = (PoolingDataSource) super.actualDataSource();
    com.vladmihalcea.flexypool.config.Configuration<PoolingDataSource> configuration = new com.vladmihalcea.flexypool.config.Configuration.Builder<>(
        getClass().getSimpleName(), poolingDataSource, BitronixPoolAdapter.FACTORY).build();

    FlexyPoolDataSource<PoolingDataSource> flexyPoolDataSource = new FlexyPoolDataSource<PoolingDataSource>(configuration) {
        @Override
        public void start() {
            poolingDataSource.init();
            super.start();
        }

        @Override
        public void stop() {
            super.stop();
            poolingDataSource.close();
        }
    };
    return flexyPoolDataSource;
}
 
Example #4
Source File: FlexyPoolHibernateConnectionProvider.java    From flexy-pool with Apache License 2.0 6 votes vote down vote up
/**
 * Substitute the already configured {@link DataSource} with the {@link FlexyPoolDataSource}
 *
 * @param props JPA/Hibernate properties
 */
@Override
public void configure(Map props) {
    super.configure(props);
    LOGGER.debug("Hibernate switched to using FlexyPoolDataSource");
    Properties overridingProperties = new Properties();
    for(Map.Entry<String, Object> propsEntry : ((Map<String, Object>) props).entrySet()) {
        if(propsEntry.getKey().startsWith( FLEXY_POOL_PROPERTY_PREFIX ) &&
            propsEntry.getValue() instanceof String) {
            overridingProperties.setProperty(
                propsEntry.getKey(),
                (String) propsEntry.getValue()
            );
        }
    }
    flexyPoolDataSource = new FlexyPoolDataSource<DataSource>(getDataSource(), overridingProperties);
}
 
Example #5
Source File: ResourceLocalFlexyPoolHibernateConnectionProviderIntegrationTest.java    From flexy-pool with Apache License 2.0 6 votes vote down vote up
@Test
public void testOverrideProperties() {
	getTransactionTemplate().execute(new TransactionCallback<Void>() {
		@Override
		public Void doInTransaction(TransactionStatus status) {
			Session session = getEntityManager().unwrap( Session.class );
			SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();
			FlexyPoolHibernateConnectionProvider flexyPoolHibernateConnectionProvider =
					(FlexyPoolHibernateConnectionProvider) sessionFactory.getConnectionProvider();
			FlexyPoolDataSource flexyPoolDataSource = ReflectionUtils.getFieldValue(
					flexyPoolHibernateConnectionProvider,
					"flexyPoolDataSource"
			);
			assertEquals( "abcd1234", ReflectionUtils.getFieldValue( flexyPoolDataSource, "uniqueName" ));

			return null;
		}
	});

}
 
Example #6
Source File: FlexyPoolConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void testDecoratingHikariDataSourceWithCustomPropertyStrategies() {
    ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("spring.datasource.type:" + HikariDataSource.class.getName(),
            "decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.max-overflow-pool-size:15",
            "decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.timeout-millis:500",
            "decorator.datasource.flexy-pool.acquiring-strategy.retry.attempts:5")
            .withUserConfiguration(HikariConfiguration.class);

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        FlexyPoolDataSource<HikariDataSource> flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class);
        IncrementPoolOnTimeoutConnectionAcquiringStrategy<HikariDataSource> strategy1 =
                findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquiringStrategy.class);
        assertThat(strategy1).isNotNull();
        assertThat(strategy1).hasFieldOrPropertyWithValue("maxOverflowPoolSize", 35);
        assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 10000);

        RetryConnectionAcquiringStrategy<HikariDataSource> strategy2 =
                findStrategy(flexyPoolDataSource, RetryConnectionAcquiringStrategy.class);
        assertThat(strategy2).isNotNull();
        assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 5);
    });
}
 
Example #7
Source File: FlexyPoolConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void testDecoratingHikariDataSourceWithDefaultStrategies() {
    ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("spring.datasource.type:" + HikariDataSource.class.getName());

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        assertDataSourceOfType(dataSource, HikariDataSource.class);
        FlexyPoolDataSource<HikariDataSource> flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class);
        IncrementPoolOnTimeoutConnectionAcquiringStrategy<HikariDataSource> strategy1 =
                findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquiringStrategy.class);
        assertThat(strategy1).isNotNull();
        assertThat(strategy1).hasFieldOrPropertyWithValue("maxOverflowPoolSize", 15);
        assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 500);

        RetryConnectionAcquiringStrategy<HikariDataSource> strategy2 =
                findStrategy(flexyPoolDataSource, RetryConnectionAcquiringStrategy.class);
        assertThat(strategy2).isNotNull();
        assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 2);
    });
}
 
Example #8
Source File: FlexyPoolWrapper.java    From hammock with Apache License 2.0 5 votes vote down vote up
private DataSource wrapInternal(String name, HikariDataSource dataSource) {
    Configuration<HikariDataSource> configuration = createConfiguration(name, config, dataSource);
    int maxOverflow = config.getOptionalValue(format(MAX_OVERFLOW_PROPERTY_FORMAT, name), Integer.class).orElse(5);
    int retry = config.getOptionalValue(format(RETRY_PROPERTY_FORMAT, name), Integer.class).orElse(2);
    return new FlexyPoolDataSource<HikariDataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(maxOverflow),
            new RetryConnectionAcquiringStrategy.Factory(retry)
    );
}
 
Example #9
Source File: MockMetricsFactory.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Override
public Metrics newInstance(ConfigurationProperties configurationProperties) {
    Mockito.reset(metrics);
    when(metrics.timer(FlexyPoolDataSource.OVERALL_CONNECTION_ACQUIRE_MILLIS)).thenReturn(overallConnectionAcquireTimer);
    when(metrics.histogram(FlexyPoolDataSource.CONCURRENT_CONNECTIONS_HISTOGRAM)).thenReturn(concurrentConnectionCountHistogram);
    when(metrics.histogram(FlexyPoolDataSource.CONCURRENT_CONNECTION_REQUESTS_HISTOGRAM)).thenReturn(concurrentConnectionRequestCountHistogram);
    when(metrics.timer(FlexyPoolDataSource.CONNECTION_LEASE_MILLIS)).thenReturn(connectionLeaseMillisTimer);
    when(metrics.timer(AbstractPoolAdapter.CONNECTION_ACQUIRE_MILLIS)).thenReturn(connectionAcquireMillisTimer);
    return metrics;
}
 
Example #10
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<PoolingDataSource> configuration = configuration();
    return new FlexyPoolDataSource<PoolingDataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #11
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 #12
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<HikariDataSource> configuration = configuration();
    return new FlexyPoolDataSource<HikariDataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #13
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<ComboPooledDataSource> configuration = configuration();
    return new FlexyPoolDataSource<ComboPooledDataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #14
Source File: FlexyPoolWrapperTest.java    From hammock with Apache License 2.0 5 votes vote down vote up
@Test
public void wrapsHikariDataSource() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl("jdbc:h2:mem:test_mem");
    DataSource dataSource = new HikariDataSource(config);
    DataSource wrapped = wrapper.wrap("", dataSource);
    assertThat(wrapped).isInstanceOf(FlexyPoolDataSource.class);
}
 
Example #15
Source File: AbstractFlexyDataSourceConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration configuration = configuration();
    return new FlexyPoolDataSource(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(getMaxOverflowPoolSize()),
            new RetryConnectionAcquiringStrategy.Factory(getRetryAttempts())
    );
}
 
Example #16
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
	Configuration<DruidDataSource> configuration = configuration();
	return new FlexyPoolDataSource<DruidDataSource>(
			configuration,
			new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory( 5 ),
			new RetryConnectionAcquiringStrategy.Factory( 2 )
	);
}
 
Example #17
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<DataSource> configuration = configuration();
    return new FlexyPoolDataSource<DataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #18
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<BasicDataSource> configuration = configuration();
    return new FlexyPoolDataSource<BasicDataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #19
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<BasicDataSource> configuration = configuration();
    return new FlexyPoolDataSource<BasicDataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #20
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<AbstractDataSourceBean> configuration = configuration();
    return new FlexyPoolDataSource<AbstractDataSourceBean>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #21
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<DataSource> configuration = configuration();
    return new FlexyPoolDataSource<DataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #22
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<ViburDBCPDataSource> configuration = configuration();
    return new FlexyPoolDataSource<ViburDBCPDataSource>(configuration,
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #23
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<AbstractDataSourceBean> configuration = configuration();
    return new FlexyPoolDataSource<AbstractDataSourceBean>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(5),
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #24
Source File: FlexyPoolConfiguration.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "stop")
public FlexyPoolDataSource dataSource() {
    Configuration<BoneCPDataSource> configuration = configuration();
    return new FlexyPoolDataSource<BoneCPDataSource>(configuration,
            new RetryConnectionAcquiringStrategy.Factory(2)
    );
}
 
Example #25
Source File: ResourceLocalDelayConnectionAcquisitionTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Override
protected DataSource newDataSource() {
	DataSource dataSource = super.newDataSource();

	com.vladmihalcea.flexypool.config.Configuration<DataSource> configuration = new com.vladmihalcea.flexypool.config.Configuration.Builder<>(
			getClass().getSimpleName(), dataSource, DataSourcePoolAdapter.FACTORY )
			.setMetricLogReporterMillis( TimeUnit.SECONDS.toMillis( 15 ) )
			.build();
	flexyPoolDataSource = new FlexyPoolDataSource<>( configuration );
	return flexyPoolDataSource;
}
 
Example #26
Source File: FlexyPoolConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends ConnectionAcquiringStrategy> T findStrategy(FlexyPoolDataSource<?> flexyPoolDataSource,
        Class<T> factoryClass) {
    Field field = ReflectionUtils.findField(FlexyPoolDataSource.class, "connectionAcquiringStrategies");
    Objects.requireNonNull(field);
    ReflectionUtils.makeAccessible(field);
    Set<ConnectionAcquiringStrategy> strategies =
            (Set<ConnectionAcquiringStrategy>) ReflectionUtils.getField(field, flexyPoolDataSource);
    Objects.requireNonNull(strategies);
    return (T) strategies.stream().filter(factoryClass::isInstance).findFirst().orElse(null);
}
 
Example #27
Source File: FlexyPoolConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends DataSource> FlexyPoolDataSource<T> assertDataSourceOfType(DataSource dataSource, Class<T> realDataSourceClass) {
    assertThat(dataSource).isInstanceOf(DecoratedDataSource.class);
    DataSource decoratedDataSource = ((DecoratedDataSource) dataSource).getDecoratedDataSource();
    assertThat(decoratedDataSource).isInstanceOf(FlexyPoolDataSource.class);
    assertThat(decoratedDataSource).extracting("targetDataSource").isInstanceOf(realDataSourceClass);
    return (FlexyPoolDataSource<T>) decoratedDataSource;
}
 
Example #28
Source File: FlexyPoolConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
void testSettingMicrometerMetricsFactoryByDefault() {
    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        FlexyPoolDataSource<HikariDataSource> flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class);
        assertThat(flexyPoolDataSource).extracting("metrics").isInstanceOf(MicrometerMetrics.class);
    });
}
 
Example #29
Source File: FlexyPoolConfigurationTests.java    From spring-boot-data-source-decorator with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void testDecoratingHikariDataSourceWithCustomBeanStrategies() {
    ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("spring.datasource.type:" + HikariDataSource.class.getName())
            .withConfiguration(AutoConfigurations.of(HikariConfiguration.class));

    contextRunner.run(context -> {
        DataSource dataSource = context.getBean(DataSource.class);
        FlexyPoolDataSource<HikariDataSource> flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class);
        IncrementPoolOnTimeoutConnectionAcquiringStrategy<HikariDataSource> strategy1 =
                findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquiringStrategy.class);
        assertThat(strategy1).isNotNull();
        assertThat(strategy1).hasFieldOrPropertyWithValue("maxOverflowPoolSize", 35);
        assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 10000);

        RetryConnectionAcquiringStrategy<HikariDataSource> strategy2 =
                findStrategy(flexyPoolDataSource, RetryConnectionAcquiringStrategy.class);
        assertThat(strategy2).isNotNull();
        assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 5);

        HikariConnectionAcquiringFactory strategy3 =
                findStrategy(flexyPoolDataSource, HikariConnectionAcquiringFactory.class);
        assertThat(strategy3).isNotNull();

        Dbcp2ConnectionAcquiringFactory strategy4 =
                findStrategy(flexyPoolDataSource, Dbcp2ConnectionAcquiringFactory.class);
        assertThat(strategy4).isNull();
    });
}
 
Example #30
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]");
    });
}