Druid
java
import com.alibaba.druid.pool.DruidDataSource;
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
dataSource.setLoginTimeout(timeout);
dataSource.setQueryTimeout(timeout);
dataSource.setInitialSize(10);
dataSource.setTestOnBorrow(true);
dataSource.setTimeBetweenEvictionRunsMillis(6000);
dataSource.setValidationQuery("select user()");
dataSource.setValidationQueryTimeout(3);
return dataSource;
Hikari
java
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
HikariConfig config = new HikariConfig();
config.setDriverClassName(driverClass);
config.setJdbcUrl(url);
config.setUsername(user);
config.setPassword(password);
config.setConnectionTimeout(timeout*1000);
config.setMaximumPoolSize(10);
config.setValidationTimeout(3000L);
config.setConnectionTestQuery("select user()");
config.setKeepaliveTime(30000L);
return new HikariDataSource(config);
dbcp2
java
import org.apache.commons.dbcp2.BasicDataSource;
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(driverClass);
ds.setUrl(url);
ds.setUsername(user);
ds.setPassword(password);
ds.setDefaultQueryTimeout(timeout);
ds.setInitialSize(10);
ds.setValidationQueryTimeout(3);
ds.setValidationQuery("select user()");
ds.setTimeBetweenEvictionRunsMillis(6000);
return ds;