Reference
https://blog.csdn.net/zhaoyachao123/article/details/78413467
https://segmentfault.com/a/1190000013997259
Preparation: Druid Alibaba open performance, versatility connection pool, the main configuration parameters are as follows:
Configuration | The default value | Explanation |
name | Configuring this attribute significance is that if multiple data sources exist to monitor when you can distinguish by area name. If no, will generate a name, the format is:. "DataSource-" + System.identityHashCode (this) This property is additionally arranged at least in version 1.0.5 is inactive, an error will be forcibly set name. Details - click here. | |
url | Url connection to the database, different databases are not the same. For example: MySQL: jdbc: MySQL: //127.0.0.1: 3306 / druid2 the Oracle: jdbc: the Oracle: Thin: @ 127.0.0.1: 1521: ocnauto |
|
username | User name to connect the database | |
password | Password for the database connection. If you do not want to write code directly in the configuration file, you can use ConfigFilter. The details look here | |
driverClassName | The url can be equipped with an automatic recognition which may be unworthy, if not based on the configuration druid automatically identify dbType url, then select the appropriate driverClassName | |
initialSize | 0 | Establishing the number of physical connection initialization. Initialization occurs when the display call the init method, or the first getConnection |
maxActive | 8 | The maximum number of connection pools |
maxIdle | 8 | It is no longer used, equipped with no effect |
minIdle | The minimum number of connection pool | |
maxWait | Acquires the maximum waiting time is connected, in milliseconds. After configuring maxWait, enabled by default fair locks, concurrent efficiency will decline, if necessary, by using the configuration useUnfairLock property to true unfair lock. | |
poolPreparedStatements | false | Whether the cache preparedStatement, which is PSCache. PSCache support database cursors huge performance boost, for example oracle. Under the proposed closure of mysql. |
maxPoolPreparedStatementPerConnectionSize | -1 | To enable PSCache, must be greater than zero configuration, when greater than 0, poolPreparedStatements triggered automatically changed to true. In the Druid, the PSCache not exist too much memory for an Oracle issue, this value can be configured larger, such as 100 |
validationQuery | For detecting whether a valid connection sql, a query is required, common select 'x'. If validationQuery is null, testOnBorrow, testOnReturn, testWhileIdle will not work. | |
validationQueryTimeout | Unit: second detecting whether a valid connection timeout. void setQueryTimeout level calls jdbc Statement object (int seconds) Method | |
testOnBorrow | true | Execution validationQuery detect whether the connection is valid when applying for connection, made this configuration reduces performance |
testOnReturn | false | Returned connection is valid execution validationQuery detect a connection, made this configuration can degrade performance. |
testWhileIdle | false | Recommended configuration is true, does not affect performance, and ensure safety. Detecting when the application connection, if the idle time is greater than timeBetweenEvictionRunsMillis, performs detection validationQuery connection is valid. |
keepAlive | false | (1.0.28) 连接池中的minIdle数量以内的连接,空闲时间超过minEvictableIdleTimeMillis,则会执行keepAlive操作。 |
timeBetweenEvictionRunsMillis | 1分钟(1.0.14) | 有两个含义: 1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。 2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明 |
numTestsPerEvictionRun | 30分钟(1.0.14) | 不再使用,一个DruidDataSource只支持一个EvictionRun |
minEvictableIdleTimeMillis | 连接保持空闲而不被驱逐的最小时间 | |
connectionInitSqls | 物理连接初始化的时候执行的sql | |
exceptionSorter | 根据dbType自动识别 | 当数据库抛出一些不可恢复的异常时,抛弃连接 |
filters | 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall |
|
proxyFilters | 类型是List<com.alibaba.druid.filter.Filter>,如果同时配置了filters和proxyFilters,是组合关系,并非替换关系 |
springboot 中整合druid 需要添加druid jar 这里spring-boot 的版本是1.5.7
添加druid maven 配置:
-
<dependency>
-
<groupId>com.alibaba
</groupId>
-
<artifactId>druid
</artifactId>
-
<version>1.1.5
</version>
-
</dependency>
Class configuration as follows: wherein uses
@Primary annotations
, annotation used in the spring, often used @Autowired, default according to the type Type automatically injected. However, some special circumstances, for the same interface, there may be several different implementation class, and the default will only take one case @Primary role came out. Here is a simple example of the use.
-
package com.zyc.config;
-
-
import java.sql.SQLException;
-
import javax.sql.DataSource;
-
import org.springframework.beans.factory.annotation.Value;
-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
-
import org.springframework.boot.web.servlet.ServletRegistrationBean;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
import org.springframework.context.annotation.Primary;
-
import com.alibaba.druid.pool.DruidDataSource;
-
import com.alibaba.druid.support.http.StatViewServlet;
-
import com.alibaba.druid.support.http.WebStatFilter;
-
-
@Configuration
-
public
class DruidConfig {
-
-
@Value(
"${spring.datasource.url}")
-
private String dbUrl;
-
-
@Value(
"${spring.datasource.username}")
-
private String username;
-
-
@Value(
"${spring.datasource.password}")
-
private String password;
-
-
@Value(
"${spring.datasource.driver-class-name}")
-
private String driverClassName;
-
-
@Value(
"${spring.datasource.initialSize}")
-
private
int initialSize;
-
-
@Value(
"${spring.datasource.minIdle}")
-
private
int minIdle;
-
-
@Value(
"${spring.datasource.maxActive}")
-
private
int maxActive;
-
-
@Value(
"${spring.datasource.maxWait}")
-
private
int maxWait;
-
-
@Value(
"${spring.datasource.timeBetweenEvictionRunsMillis}")
-
private
int timeBetweenEvictionRunsMillis;
-
-
@Value(
"${spring.datasource.minEvictableIdleTimeMillis}")
-
private
int minEvictableIdleTimeMillis;
-
-
@Value(
"${spring.datasource.validationQuery}")
-
private String validationQuery;
-
-
@Value(
"${spring.datasource.testWhileIdle}")
-
private
boolean testWhileIdle;
-
-
@Value(
"${spring.datasource.testOnBorrow}")
-
private
boolean testOnBorrow;
-
-
@Value(
"${spring.datasource.testOnReturn}")
-
private
boolean testOnReturn;
-
-
@Value(
"${spring.datasource.poolPreparedStatements}")
-
private
boolean poolPreparedStatements;
-
-
@Value(
"${spring.datasource.filters}")
-
private String filters;
-
-
@Value(
"${spring.datasource.logSlowSql}")
-
private String logSlowSql;
-
-
@Bean
-
@Primary
-
public DataSource dataSource(){
-
//@Primary 注解作用是当程序选择dataSource时选择被注解的这个
-
DruidDataSource datasource =
new DruidDataSource();
-
datasource.setUrl(dbUrl);
-
datasource.setUsername(username);
-
datasource.setPassword(password);
-
datasource.setDriverClassName(driverClassName);
-
datasource.setInitialSize(initialSize);
-
datasource.setMinIdle(minIdle);
-
datasource.setMaxActive(maxActive);
-
datasource.setMaxWait(maxWait);
-
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
-
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
-
datasource.setValidationQuery(validationQuery);
-
datasource.setTestWhileIdle(testWhileIdle);
-
datasource.setTestOnBorrow(testOnBorrow);
-
datasource.setTestOnReturn(testOnReturn);
-
datasource.setPoolPreparedStatements(poolPreparedStatements);
-
try {
-
datasource.setFilters(filters);
-
}
catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
return datasource;
-
}
-
-
@Bean
-
public ServletRegistrationBean druidServlet() {
-
ServletRegistrationBean reg =
new ServletRegistrationBean();
-
reg.setServlet(
new StatViewServlet());
-
reg.addUrlMappings(
"/druid/*");
-
reg.addInitParameter(
"loginUsername", username);
-
reg.addInitParameter(
"loginPassword", password);
-
reg.addInitParameter(
"logSlowSql", logSlowSql);
-
return reg;
-
}
-
-
@Bean
-
public FilterRegistrationBean filterRegistrationBean() {
-
FilterRegistrationBean filterRegistrationBean =
new FilterRegistrationBean();
-
filterRegistrationBean.setFilter (
new WebStatFilter());
-
filterRegistrationBean.addUrlPatterns(
"/*");
-
filterRegistrationBean.addInitParameter (
"exclusions",
"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
-
filterRegistrationBean.addInitParameter (
"profileEnable",
"true");
-
return filterRegistrationBean;
-
}
-
}
application.properties configuration is as follows:
-
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
-
spring.datasource.url = jdbc:oracle:thin:@
127.0.0.1/orcl
-
spring.datasource.username = root
-
spring.datasource.password =
123456
-
spring.datasource.driver-
class-name = oracle.jdbc.driver.OracleDriver
-
spring.datasource.filters = stat,wall,log4j
-
spring.datasource.maxActive =
20
-
spring.datasource.initialSize =
1
-
spring.datasource.maxWait =
60000
-
spring.datasource.minIdle =
1
-
spring.datasource.timeBetweenEvictionRunsMillis =
60000
-
spring.datasource.minEvictableIdleTimeMillis =
300000
-
spring.datasource.validationQuery = select
'x'
-
spring.datasource.testWhileIdle =
true
-
spring.datasource.testOnBorrow =
false
-
spring.datasource.testOnReturn =
false
-
spring.datasource.poolPreparedStatements =
true
-
spring.datasource.maxOpenPreparedStatements =
20
-
spring.datasource.logSlowSql=
true
spring.datasource.type 是spring-boot 指定datasource类型的属性不是druid的属性,使用时需要注意,个别版本的spring-boot 不支持spring.datasource.type这个属性建议使用1.4.1版本以上的spring-boot
启动项目访问http://ip:port/druid/login.html