Skip to content

Vibur DBCP Configuration

Vlad Mihalcea edited this page Jun 21, 2015 · 3 revisions

Maven dependency

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

Data Source Specific Configuration

<bean id="poolingDataSource" class="org.vibur.dbcp.ViburDBCPDataSource" init-method="start" destroy-method="terminate">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="poolInitialSize" value="0"/>
        <property name="poolMaxSize" value="3"/>
        <property name="connectionTimeoutInMs" value="100"/>
    </bean>

You should not use IncrementPoolOnTimeoutConnectionAcquiringStrategy because Vibur DBCP does not support pool resizing natively, as C3P0. This way, it’s impossible to guarantee what will happen to the current acquired connections one the pool has to be destroyed and recreated, only to take into consideration the new pool size.

If you use it, you’ll get an UnsupportedOperationException whenever the max pool size is about to be changed.

Spring Java-Bean Configuration

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

    @Autowired
    private ViburDBCPDataSource poolingDataSource;

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

    @Bean
    public Configuration<ViburDBCPDataSource> configuration() {
        return new Configuration.Builder<ViburDBCPDataSource>(
                uniqueId,
                poolingDataSource,
                ViburDBCPPoolAdapter.FACTORY
        ).build();
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public FlexyPoolDataSource dataSource() {
        Configuration<ViburDBCPDataSource> configuration = configuration();
        return new FlexyPoolDataSource<ViburDBCPDataSource>(configuration,
                new RetryConnectionAcquiringStrategy.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.ViburDBCPPoolAdapter).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.RetryConnectionAcquiringStrategy.Factory">
                <constructor-arg value="2"/>
            </bean>
        </array>
    </constructor-arg>
</bean>