1. The data source

A DataSource is a JDBC specification interface defined by SUN for obtaining database connections. It is in the javax. SQL package and is used instead of DriverManager to get connections.

package javax.sql; import java.sql.Connection; import java.sql.SQLException; import java.sql.Wrapper; Public Interface DataSource extends CommonDataSource, Wrapper {Connection getConnection() throws SQLException; Connection getConnection(String username, String password) throws SQLException; }Copy the code

Using data sources can directly obtain the database connection object, do not need to write database code.

2. Database connection pool

A data source can create multiple database connections, which are stored in a database connection pool. When you need to establish a database connection, you simply take a free connection from the connection pool and put it back when it is used up.

Traditional JDBC access technology, each access to the database through the database Driver and database name and password and other resources to establish a database connection, will increase the cost of system resources and CPU, and connection pool can be a good solution to this problem.

In addition, we can monitor the number and usage of database connections through the connection pool management mechanism, providing basis for system development, testing and performance adjustment.

3. Integrate the Druid connection pool

Druid connection pool Druid connection pool Druid connection pool Druid connection pool Druid connection pool Druid connection pool Druid connection pool Druid connection pool Druid connection pool

SpringBoot integrates with Druid connection pools in two main ways.

(1) Use the Druid initiator

Druid-spring-boot-starter in pum. XML and application.yaml

< the dependency > < groupId > com. Alibaba < / groupId > < artifactId > druid - spring - the boot - starter < / artifactId > < version > 1.1.22 < / version >  </dependency>Copy the code
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 username: root password: rootCopy the code

Write a simple test case that prints the configuration of the data source:

@SpringBootTest @RunWith(SpringRunner.class) public class DruidStarterConfigTest { @Resource private DataSource dataSource; @Test public void test() throws SQLException { DruidDataSource druidDataSource = (DruidDataSource) dataSource; System.out.println(druidDataSource.getDriverClassName()); System.out.println(druidDataSource.getUrl()); System.out.println(druidDataSource.getUsername()); System.out.println(druidDataSource.getPassword()); }}Copy the code

Running results:

(2) Customize Druid connection pool

Import the data source in pom.xml and configure it in application.yaml:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.5</version>
</dependency>
Copy the code

Without a Druid initiator, we need to manually inject a Druid data source ourselves, using @ConfigurationProperties to inject the properties configured in application.yaml.

@Configuration public class DruidStarterConfig { @Bean @ConfigurationProperties("spring.datasource") public DataSource druidDataSource() { return new DruidDataSource(); }}Copy the code

Again, use the above test case to see the results:

@SpringBootTest @RunWith(SpringRunner.class) public class DruidStarterConfigTest { @Resource private DataSource dataSource; @Test public void test() throws SQLException { DruidDataSource druidDataSource = (DruidDataSource) dataSource; System.out.println(druidDataSource.getDriverClassName()); System.out.println(druidDataSource.getUrl()); System.out.println(druidDataSource.getUsername()); System.out.println(druidDataSource.getPassword()); }}Copy the code

4. Druid Connection pool properties

In addition to the properties configured above, Druid provides a number of connection pool properties for better management and monitoring of connection pools, as shown here.

spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test? ServerTimezone =UTC&useUnicode=true&characterEncoding= UTF-8 username: root password: root #druid Min-idle: 5 # Maximum number of connection pools maxIdle Is no longer in use max-active: 20 # Maximum waiting time for obtaining connections, in milliseconds max-wait: 60000 # application connection testing, if free time is more than timeBetweenEvictionRunsMillis, performing validationQuery test connection is valid. Test-while-idle: true # testWhileIdel execution is supported by time-in-eviction -runs-millis, which is designed to simulate the performance of a particular object. Min-evictable-idle-time-millis: < span style = "max-width: 100%; clear: 100%; Mysql > select * from dual validation-query; mysql > select * from dual validation-query Select 'x' # from 'x' where validationQuery is executed to check whether the connection is valid. False # Discard connection exception-sorter when the database throws an unrecoverable exception: Whether true # cache preparedStatement, mysql5.5 + suggest open the pool - prepared statements: If the value is greater than 0, poolPreparedStatements will automatically change to true max-pool-prepared-statement-per-connection-size: 20Copy the code

I’m not going to cover the rest of Druid’s functionality in this article because I don’t have enough space.

Refer to the reference

Data source: blog.csdn.net/dragon9… Data source and connection pool: www.cnblogs.com/chens… Druid data source usage: juejin.cn/post/699320… Druid initiator: juejin.cn/post/699355…