springboot连接池Druid、Hikari、dbcp2配置dataSource

奋斗吧
奋斗吧
擅长邻域:未填写

标签: springboot连接池Druid、Hikari、dbcp2配置dataSource

2023-07-26 18:23:41 157浏览

【代码】springboot连接池Druid、Hikari、dbcp2配置dataSource。

Druid

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

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

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;

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695