前言:
druid 连接池是阿里巴巴开源的数据库连接池项目,druid 为监控而生,内置强大的监控功能,监控特性不影响性能,官网地址为:/d/file/titlepic/ style="background-color: initial">本文主要介绍 druid 的基本使用,文中使用到的软件版本:java 1.8.0_191、druid 1.2.8、spring boot 2.3.12.relea。
druid
连接池的配置参数兼容 dbcp
,个别配置的语意有所区别。
<dependency> <groupid>com.alibaba</gro最长的歌词upid> <artifactid>druid</artifactid> <version>1.2.8</version></dependency>
package com.abc.demo.general.dbpool;import com.alibaba.druid.pool.druiddatasource;import java.sql.connection;import java.sql.resultt;import java.sql.sqlexception;import java.sql.statement;public class druidca { public static void main(string[] args) { druiddatasource druiddatasource = new druiddatasource(); connection connection = null; try { druiddatasource.tname("测试连接池"); druiddatasource.tdriverclassname("com.mysql.cj.jdbc.driver"); druiddatasource.turl("jdbc:mysql://10.40.9.11:3306/mydb?uunicode=true&characterencoding=utf-8"); druiddatasource.turname("root"); druiddatasource.tpassword("123456"); druiddatasource.tinitialsize(2); druiddatasource.tminidle(2); druiddatasource.tmaxactive(5); druiddatasource.tvalidationquery("lect 1"); druiddatasource.ttestwhileidle(true); druiddatasource.ttestonborrow(true); druiddatasource.ttestonreturn(fal); druiddatasource.tmaxwait(6000); druiddatasource.tfilters("slf4j"); connection = druiddatasource.getconnection(); statement st = connection.createstatement(); resultt rs = st.executequery("lect version()"); if (rs.next()) { system.out.println(rs.getstring(1)); } } catch (sqlexception e) { e.printstacktrace(); } finally { clo(connection); } //实际使用中一般是在应用启动时初始化数据源,应用从数据源中获取连接;并不会关闭数据源。 druiddatasource.clo(); } private static void clo(connection connection) { if (connection != null) { try { connection.clo(); } catch (sqlexception e) { e.printstacktrace(); } } }}
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.3.12.relea</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&游记范文gt;mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency></dependencies>
application.yml 配置:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://10.40.9.11:3306/mydb?uunicode=true&characterencoding=utf-8 urname: root password: 123456 type: com.alibaba.druid.pool.druiddatasource druid: initial-size: 2 min-idle: 2 max-active: 5 validation-query: lect 1 test-while-idle: true test-on-borrow: true test-on-return: fal max-wait: 6000 filter: slf4j
使用:
@autowiredprivate datasource datasource;
application.yml高三物理 配置:
spring: datasource: druid: db1: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://10.140.9.11:3306/mydb?uunicode=true&characterencoding=utf-8 urname: root password: 123456 initial-size: 2 六年级作文大全 min-idle: 2 max-active: 5 validation-query: lect 1 test-while-idle: true test-on-borrow: true test-on-return: fal max-wait: 6000 filter: slf4j db2: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://10.40.9.12:3306/mydb?uunicode=true&characterencoding=utf-8 urname: root password: 123456 initial-size: 2 min-idle: 2 max-active: 5 validation-query: lect 1 test-while-idle: true test-on-borrow: true test-on-return: fal 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;@configurationpublic 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;
druid
内置了一些监控,spring boot
环境下通过少量配置就可开启这些功能。
application.yml 配置:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc:mysql://10.40.9.11:3306/mydb?uunicode=true&characterencoding=utf-8 urname: root p电烙铁的使用方法assword: insyr0ot187! type: com.alibaba.druid.pool.druiddatasource druid: initial-size: 2 min-idle: 2 max-active: 5 validation-query: lect 1 test-while-idle: true test-on-borrow: true test-on-return: fal max-wait: 6000 filter: slf4j,stat,wall #启用日志、监控统计、防火墙功能 web-stat-filter: #web监控配置 enabled: true stat-view-rvlet: #监控页面配置 enabled: true url-pattern: /druid/*
监控页面:
spring boot
中集成 druid 的详细说明可以参考官网文档:https://hub.fastgit.org/alibaba/druid/tree/master/druid-spring-boot-starter
到此这篇关于 java 数据库连接池druid 的介绍的文章就介绍到这了,更多相关 java连接池druid 内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-03 22:18:02,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1ec6eb7380ff472631b434c4b7a5aa88.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java 数据库连接池Druid 的介绍.doc
本文 PDF 下载地址:Java 数据库连接池Druid 的介绍.pdf
留言与评论(共有 0 条评论) |