dbcp连接池配置详解_Druid连接池的使用

Druid是阿里开源的数据库连接池,作为后起之秀,性能比dbcp、c3p0更高,使用也越来越广泛。Druid简介

当然Druid不仅仅是一个连接池,还有很多其他的功能。

druid的优点
  • 高性能。性能比dbcp、c3p0高很多。

  • 只要是jdbc支持的数据库,druid都支持,对数据库的支持性好。并且Druid针对oracle、mysql做了特别优化。

  • 提供监控功能。可以监控sql语句的执行时间、ResultSet持有时间、返回行数、更新行数、错误次数、错误堆栈等信息,来了解连接池、sql语句的工作情况,方便统计、分析SQL的执行性能

Druid的使用

添加druid的依赖、数据库驱动

b7565229720db787d8481e8397a3e8fe.png

        <dependency><groupId>com.alibabagroupId><artifactId>druidartifactId><version>1.1.8version>dependency><dependency><groupId>mysqlgroupId><artifactId>mysql-connector-javaartifactId><version>8.0.19version>dependency>    

b7565229720db787d8481e8397a3e8fe.png

纯代码方式

b7565229720db787d8481e8397a3e8fe.png

    //数据源配置
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mysql://127.0.0.1/db_student?serverTimezone=UTC");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); //这个可以缺省的,会根据url自动识别
dataSource.setUsername("root");
dataSource.setPassword("abcd");//下面都是可选的配置
dataSource.setInitialSize(10); //初始连接数,默认0
dataSource.setMaxActive(30); //最大连接数,默认8
dataSource.setMinIdle(10); //最小闲置数
dataSource.setMaxWait(2000); //获取连接的最大等待时间,单位毫秒
dataSource.setPoolPreparedStatements(true); //缓存PreparedStatement,默认false
dataSource.setMaxOpenPreparedStatements(20); //缓存PreparedStatement的最大数量,默认-1(不缓存)。大于0时会自动开启缓存PreparedStatement,所以可以省略上一句代码
//获取连接
Connection connection = dataSource.getConnection();//Statement接口
Statement statement = connection.createStatement();
String sql1 = "insert into tb_student (name,age) values ('chy',20)";
statement.executeUpdate(sql1);//PreparedStatement接口
String sql2 = "insert into tb_student (name,age) values ('chy',21)";
PreparedStatement preparedStatement = connection.prepareStatement(sql2);
preparedStatement.execute();//关闭连接
connection.close();

b7565229720db787d8481e8397a3e8fe.png

配置文件方式

1、在sources下新建druid.properties

b7565229720db787d8481e8397a3e8fe.png

url=jdbc:mysql://127.0.0.1/db_student?serverTimezone=UTC
#这个可以缺省的,会根据url自动识别
driverClassName=com.mysql.cj.jdbc.Driver
username=root
password=abcd
##初始连接数,默认0
initialSize=10
#最大连接数,默认8
maxActive=30
#最小闲置数
minIdle=10
#获取连接的最大等待时间,单位毫秒
maxWait=2000
#缓存PreparedStatement,默认false
poolPreparedStatements=true
#缓存PreparedStatement的最大数量,默认-1(不缓存)。大于0时会自动开启缓存PreparedStatement,所以可以省略上一句设置
maxOpenPreparedStatements=20

b7565229720db787d8481e8397a3e8fe.png

2、使用

b7565229720db787d8481e8397a3e8fe.png

public class Test {public static void main(String[] args) throws Exception {//数据源配置
Properties properties=new Properties();//通过当前类的class对象获取资源文件
InputStream is = Test.class.getResourceAsStream("/druid.properties");
properties.load(is);//返回的是DataSource,不是DruidDataSource
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);//获取连接
Connection connection = dataSource.getConnection();//Statement接口
Statement statement = connection.createStatement();
String sql1 = "insert into tb_student (name,age) values ('chy',20)";
statement.executeUpdate(sql1);//PreparedStatement接口
String sql2 = "insert into tb_student (name,age) values ('chy',22)";
PreparedStatement preparedStatement = connection.prepareStatement(sql2);
preparedStatement.execute();//关闭连接 connection.close();
}
}

b7565229720db787d8481e8397a3e8fe.png

这种方式对properties的key有严格要求,必须值指定的字符串,容易写错。

如果觉得老调不好,可以DruidDataSource dataSource = new DruidDataSource();  再调用setter方法使用配置文件的值,不过很麻烦。

在Spring中使用Druid

1、resources下新建druid.properties

b7565229720db787d8481e8397a3e8fe.png

druid.url=jdbc:mysql://127.0.0.1/db_student?serverTimezone=UTC
#这个可以缺省的,会根据url自动识别
druid.driverClassName=com.mysql.cj.jdbc.Driver
druid.username=root
druid.password=abcd
##初始连接数,默认0
druid.initialSize=10
#最大连接数,默认8
druid.maxActive=30
#最小闲置数
druid.minIdle=10
#获取连接的最大等待时间,单位毫秒
druid.maxWait=2000
#缓存PreparedStatement,默认false
druid.poolPreparedStatements=true
#缓存PreparedStatement的最大数量,默认-1(不缓存)。大于0时会自动开启缓存PreparedStatement,所以可以省略上一句设置
druid.maxOpenPreparedStatements=20

b7565229720db787d8481e8397a3e8fe.png

2、spring配置文件

b7565229720db787d8481e8397a3e8fe.png

    <context:property-placeholder location="classpath:druid.properties" /><bean name="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${druid.url}" /><property name="driverClassName" value="${druid.driverClassName}" /><property name="username" value="${druid.username}" /><property name="password" value="${druid.password}" /><property name="initialSize" value="${druid.initialSize}"/><property name="maxActive" value="${druid.maxActive}" /><property name="minIdle" value="${druid.minIdle}" /><property name="maxWait" value="${druid.maxWait}" /><property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" /><property name="maxOpenPreparedStatements" value="${druid.maxOpenPreparedStatements}" />bean>

b7565229720db787d8481e8397a3e8fe.png

注意要配置包扫描,扫描注解。

3、使用

b7565229720db787d8481e8397a3e8fe.png

@Repositorypublic class Xxx {
@Resourceprivate DruidDataSource dataSource;public void xxx() throws SQLException {//获取连接
Connection connection = dataSource.getConnection();//Statement接口
Statement statement = connection.createStatement();
String sql1 = "insert into tb_student (name,age) values ('chy',20)";
statement.executeUpdate(sql1);//PreparedStatement接口
String sql2 = "insert into tb_student (name,age) values ('chy',22)";
PreparedStatement preparedStatement = connection.prepareStatement(sql2);
preparedStatement.execute();//关闭连接 connection.close();
}
}

b7565229720db787d8481e8397a3e8fe.png

使用durid的监控功能

1、在druid数据源里启用stat过滤器

2、在web.xml中配置StatViewServlet

b7565229720db787d8481e8397a3e8fe.png

  DruidStatViewclass>com.alibaba.druid.support.http.StatViewServletclass>DruidStatView/druid/*

b7565229720db787d8481e8397a3e8fe.png

启动web应用,在  localhost:8080/druid  可看到sql的执行情况统计。(注意不是项目下)

druid默认会清空统计数据,所以只能看到当时的情况,应用需要持续操作数据库才好看到效果。

a037bf94881c3582eaaa1048641ee14a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值