SpringBoot 2統合3種類の接続プールc 3 p 0 hikari druid

8638 ワード

Hikari
1、まずhikari springbootのデフォルト統合を統合し、簡単な構成だけでよい
1.1パッケージの最初のインポート

	com.zaxxer
	HikariCP


	org.springframework.boot
	spring-boot-starter-jdbc


1.2プロファイルの作成
# hikari
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot2
#spring.datasource.username=root
#spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver

1.3構成クラスの作成
package com.archibaldwitwicke.springboot2.chapter05.dao;

import org.springframework.context.annotation.Configuration;

@Configuration
public class DataSourceConfiguration {

    // Hikari    
//    @Bean(name = "dataSource")
//    public DataSource dataSource(@Autowired Environment environment) {
//        HikariDataSource ds = new HikariDataSource();
//        ds.setJdbcUrl(environment.getProperty("spring.datasource.url"));
//        ds.setUsername(environment.getProperty("spring.datasource.username"));
//        ds.setPassword(environment.getProperty("spring.datasource.password"));
//        ds.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
//        return ds;
//    }
}

1.4 jdbctemplateテンプレートで使用できます.
 
C3P0
1該当パッケージのインポート

	c3p0
	c3p0
	0.9.1.2


	org.springframework.boot
	spring-boot-starter-jdbc

2対応するプロファイルの作成
# c3p0
#c3p0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/springboot2
#c3p0.user=root
#c3p0.password=root
#c3p0.driverClass=com.mysql.jdbc.Driver
#c3p0.minPoolSize=2
#c3p0.maxPoolSize=10
#c3p0.maxIdleTime=1800000
#c3p0.acquireIncrement=3
#c3p0.maxStatements=1000
#c3p0.initialPoolSize=3
#c3p0.idleConnectionTestPeriod=60
#c3p0.acquireRetryAttempts=30
#c3p0.acquireRetryDelay=1000
#c3p0.breakAfterAcquireFailure=false
#c3p0.testConnectionOnCheckout=false

3データソースクラスの作成
package com.archibaldwitwicke.springboot2.chapter05.dao;

import org.springframework.context.annotation.Configuration;

@Configuration
public class DataSourceConfiguration {

    // c3p0    
//    @Bean(name = "dataSource")
//    @Qualifier(value = "dataSource")
//    @Primary
//    @ConfigurationProperties(prefix = "c3p0")
//    public DataSource dataSource(@Autowired Environment environment) {
//        return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();
//    }
}

4 jdbctemplateオペレーションデータベースに注入
 
Druid
1対応するパッケージをインポート(ログパッケージが必要)

	com.alibaba
	druid
	1.0.29


	log4j
	log4j
	1.2.16
	compile


	mysql
	mysql-connector-java


	org.springframework.boot
	spring-boot-starter-jdbc


2プロファイルの作成
# durid
#       
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/springboot2
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#        
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

log 4 j構成も必要です.
log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout

### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n

### set package ###
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug

3データソースクラスの作成
package com.archibaldwitwicke.springboot2.chapter05.dao;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
public class DruidDataSourceConfiguration {
//    private Logger logger = LoggerFactory.getLogger(DruidDataSourceConfiguration.class);

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driverClassName}")
    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.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Value("{spring.datasource.connectionProperties}")
    private String connectionProperties;

    @Bean     //    Bean  
    @Primary  //    DataSource ,        DataSource
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //configuration
        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);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
//            logger.error("druid configuration initialization filter", e);
            e.printStackTrace();
        }
        datasource.setConnectionProperties(connectionProperties);

        return datasource;
    }
}

4 jdbctemplateを使用可能
 
以上のすべてのデータソースは、必要に応じてデータドライバをインポートし、msyqlをテストします.