Skip to content

Druid Configuration

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

Maven dependency

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

Data Source Specific Configuration

<bean id="poolingDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
	<property name="url" value="${jdbc.url}"/>
	<property name="username" value="${jdbc.username}"/>
	<property name="password" value="${jdbc.password}"/>

	<property name="initialSize" value="1"/>
	<property name="minIdle" value="1"/>
	<property name="maxActive" value="3"/>

	<property name="maxWait" value="1000"/>

	<property name="timeBetweenEvictionRunsMillis" value="60000"/>

	<property name="minEvictableIdleTimeMillis" value="300000"/>

	<property name="testWhileIdle" value="true"/>
	<property name="testOnBorrow" value="false"/>
	<property name="testOnReturn" value="false"/>

	<property name="poolPreparedStatements" value="true"/>
	<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>

	<property name="filters" value="stat"/>
</bean>

Spring Java-Bean Configuration

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

	@Autowired
	private DruidDataSource poolingDataSource;

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

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

	@Bean(initMethod = "start", destroyMethod = "stop")
	public FlexyPoolDataSource dataSource() {
		Configuration<DruidDataSource> configuration = configuration();
		return new FlexyPoolDataSource<DruidDataSource>(
				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.DruidAdapter).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>