Java连接池Druid
咏吟 人气:0前言:
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 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
加载全部内容