Skip to content

DBCP2 Configuration

Vlad Mihalcea edited this page Oct 17, 2024 · 4 revisions

Maven dependency

<dependency>
	<groupId>com.vladmihalcea.flexy-pool</groupId>
	<artifactId>flexy-dbcp2</artifactId>
	<version>${flexy-pool.version}</version>
</dependency>

Data Source Specific Configuration

<bean id="poolingDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driverClassName}" />
	<property name="url" value="${jdbc.url}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
	<property name="maxTotal" value="3"/>
	<property name="maxWaitMillis" value="1000"/>
</bean>

Spring Java-Bean Configuration

@org.springframework.context.annotation.Configuration
public class FlexyPoolBeanConfiguration {

    @Autowired
    private BasicDataSource poolingDataSource;

    @Value("${flexy.pool.uniqueId}")
    private String uniqueId;

    @Bean
    public FlexyPoolConfiguration<BasicDataSource> configuration() {
        return new FlexyPoolConfiguration.Builder<BasicDataSource>(
                uniqueId,
                poolingDataSource,
                DBCP2PoolAdapter.FACTORY
        ).build();
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public FlexyPoolDataSource dataSource() {
        FlexyPoolConfiguration<BasicDataSource> configuration = configuration();
        return new FlexyPoolDataSource<BasicDataSource>(configuration,
                new IncrementPoolOnTimeoutConnectionAcquisitionStrategy.Factory(5),
                new RetryConnectionAcquisitionStrategy.Factory(2)
        );
    }
}

Spring XML Configuration

<bean id="configurationBuilder" class="com.vladmihalcea.flexypool.config.Configuration$Builder">
	<constructor-arg value="uniqueId"/>
	<constructor-arg ref="poolingDataSource"/>
	<constructor-arg value="#{ T(com.vladmihalcea.flexypool.adaptor.DBCP2PoolAdapter).FACTORY }"/>
</bean>

<bean id="configuration" factory-bean="configurationBuilder" factory-method="build"/>

<bean id="dataSource" class="com.vladmihalcea.flexypool.FlexyPoolDataSource" init-method="start" destroy-method="stop">
	<constructor-arg ref="configuration"/>
	<constructor-arg>
		<array>
			<bean class="com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquisitionStrategy.Factory">
				<constructor-arg value="5"/>
			</bean>
			<bean class="com.vladmihalcea.flexypool.strategy.RetryConnectionAcquisitionStrategy.Factory">
				<constructor-arg value="2"/>
			</bean>
		</array>
	</constructor-arg>
</bean>