Java 数据库连接池Druid 的介绍
程序员文章站
2022-06-17 20:08:19
目录1、配置参数2、使用2.1、直接使用2.1.1、引入依赖2.1.2、使用例子2.2、在 springboot 中使用2.1.1、引入依赖2.1.2、单数据源2.1.3、多数据源2.1.4、开启监控...
前言:
druid 连接池是阿里巴巴开源的数据库连接池项目,druid 为监控而生,内置强大的监控功能,监控特性不影响性能,官网地址为:https://github.com/alibaba/druid/wiki/,本文主要介绍 druid 的基本使用,文中使用到的软件版本:java 1.8.0_191、druid 1.2.8、spring boot 2.3.12.release。
1、配置参数
druid
连接池的配置参数兼容 dbcp
,个别配置的语意有所区别。
2、使用
2.1、直接使用
2.1.1、引入依赖
<dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>1.2.8</version> </dependency>
2.1.2、使用例子
package com.abc.demo.general.dbpool; import com.alibaba.druid.pool.druiddatasource; import java.sql.connection; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class druidcase { public static void main(string[] args) { druiddatasource druiddatasource = new druiddatasource(); connection connection = null; try { druiddatasource.setname("测试连接池"); druiddatasource.setdriverclassname("com.mysql.cj.jdbc.driver"); druiddatasource.seturl("jdbc:mysql://10.40.9.11:3306/mydb?useunicode=true&characterencoding=utf-8"); druiddatasource.setusername("root"); druiddatasource.setpassword("123456"); druiddatasource.setinitialsize(2); druiddatasource.setminidle(2); druiddatasource.setmaxactive(5); druiddatasource.setvalidationquery("select 1"); druiddatasource.settestwhileidle(true); druiddatasource.settestonborrow(true); druiddatasource.settestonreturn(false); druiddatasource.setmaxwait(6000); druiddatasource.setfilters("slf4j"); connection = druiddatasource.getconnection(); statement st = connection.createstatement(); resultset rs = st.executequery("select version()"); if (rs.next()) { system.out.println(rs.getstring(1)); } } catch (sqlexception e) { e.printstacktrace(); } finally { close(connection); } //实际使用中一般是在应用启动时初始化数据源,应用从数据源中获取连接;并不会关闭数据源。 druiddatasource.close(); } private static void close(connection connection) { if (connection != null) { try { connection.close(); } catch (sqlexception e) { e.printstacktrace(); } } } }
2.2、在 springboot 中使用
2.1.1、引入依赖
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.3.12.release</version> <relativepath /> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid-spring-boot-starter</artifactid> <version>1.2.8</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> </dependencies>
2.1.2、单数据源
application.yml 配置:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://10.40.9.11:3306/mydb?useunicode=true&characterencoding=utf-8 username: root password: 123456 type: com.alibaba.druid.pool.druiddatasource druid: initial-size: 2 min-idle: 2 max-active: 5 validation-query: select 1 test-while-idle: true test-on-borrow: true test-on-return: false max-wait: 6000 filter: slf4j
使用:
@autowired private datasource datasource;
2.1.3、多数据源
application.yml 配置:
spring: datasource: druid: db1: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://10.140.9.11:3306/mydb?useunicode=true&characterencoding=utf-8 username: root password: 123456 initial-size: 2 min-idle: 2 max-active: 5 validation-query: select 1 test-while-idle: true test-on-borrow: true test-on-return: false max-wait: 6000 filter: slf4j db2: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://10.40.9.12:3306/mydb?useunicode=true&characterencoding=utf-8 username: root password: 123456 initial-size: 2 min-idle: 2 max-active: 5 validation-query: select 1 test-while-idle: true test-on-borrow: true test-on-return: false max-wait: 6000 filter: slf4j
数据源配置类:
package com.abc.demo.config; import com.alibaba.druid.spring.boot.autoconfigure.druiddatasourcebuilder; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.primary; import javax.sql.datasource; @configuration public class datasourceconfig { @primary @bean("datasource1") @configurationproperties("spring.datasource.druid.db1") public datasource datasourceone(){ return druiddatasourcebuilder.create().build(); } @bean("datasource2") @configurationproperties("spring.datasource.druid.db2") public datasource datasourcetwo(){ return druiddatasourcebuilder.create().build(); } }
使用:
@autowired @qualifier("datasource1") private datasource datasource1; @autowired @qualifier("datasource2") private datasource datasource2;
2.1.4、开启监控功能
druid
内置了一些监控,spring boot
环境下通过少量配置就可开启这些功能。
application.yml 配置:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://10.40.9.11:3306/mydb?useunicode=true&characterencoding=utf-8 username: root password: insyr0ot187! type: com.alibaba.druid.pool.druiddatasource druid: initial-size: 2 min-idle: 2 max-active: 5 validation-query: select 1 test-while-idle: true test-on-borrow: true test-on-return: false max-wait: 6000 filter: slf4j,stat,wall #启用日志、监控统计、防火墙功能 web-stat-filter: #web监控配置 enabled: true stat-view-servlet: #监控页面配置 enabled: true url-pattern: /druid/*
监控页面:
spring boot
中集成 druid 的详细说明可以参考官网文档:https://hub.fastgit.org/alibaba/druid/tree/master/druid-spring-boot-starter
到此这篇关于 java 数据库连接池druid 的介绍的文章就介绍到这了,更多相关 java连接池druid 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!