SpringBoot2 integration of three connection pools c3p0 Hikari Druid

Source: Internet
Author: User
Tags log4j

Hikari

1, first integrated Hikari springboot default integration, only need simple configuration can

1.1 First pilot Pack

<dependency><groupid>com.zaxxer</groupid><artifactid>hikaricp</artifactid></ Dependency><dependency><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-starter-jdbc</artifactid></dependency>

1.2 Writing the configuration file

# 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 Writing configuration Classes

Package Com.archibaldwitwicke.springboot2.chapter05.dao;import org.springframework.context.annotation.Configuration; @Configurationpublic class Datasourceconfiguration {    // Hikari Connection Pool//    @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 You can then use the JdbcTemplate template.

C3p0

1 Importing the appropriate package

<dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version> 0.9.1.2</version></dependency><dependency><groupid>org.springframework.boot</ Groupid><artifactid>spring-boot-starter-jdbc</artifactid></dependency>

2 Writing the appropriate configuration file

# 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 Writing data source classes

Package Com.archibaldwitwicke.springboot2.chapter05.dao;import org.springframework.context.annotation.Configuration; @Configurationpublic class Datasourceconfiguration {    // C3P0 Connection Pool//    @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 You can then inject the JdbcTemplate operational database

Druid

1 Importing the appropriate package (requires a log package)

<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId>< version>1.0.29</version></dependency><dependency><groupid>log4j</groupid>< Artifactid>log4j</artifactid><version>1.2.16</version><scope>compile</scope> </dependency><dependency><groupId>mysql</groupId><artifactId> Mysql-connector-java</artifactid></dependency><dependency><groupid> org.springframework.boot</groupid><artifactid>spring-boot-starter-jdbc</artifactid></ Dependency>

2 Writing the configuration file

# durid# Driver configuration Information Spring.datasource.url = Jdbc:mysql://127.0.0.1:3306/springboot2spring.datasource.username = Rootspring.datasource.password = Rootspring.datasource.driverClassName = com.mysql.jdbc.driver# Configuration information for the connection pool spring.datasource.initialsize=5spring.datasource.minidle=5spring.datasource.maxactive= 20spring.datasource.maxwait=60000spring.datasource.timebetweenevictionrunsmillis= 60000spring.datasource.minevictableidletimemillis=300000spring.datasource.validationquery=select 1 from dualspring.datasource.testwhileidle=truespring.datasource.testonborrow=falsespring.datasource.testonreturn= Falsespring.datasource.poolpreparedstatements=truespring.datasource.maxpoolpreparedstatementperconnectionsize= 20spring.datasource.filters=stat,wall,log4jspring.datasource.connectionproperties=druid.stat.mergesql=true; druid.stat.slowsqlmillis=5000

LOG4J configuration is also required:

Log4j.rootcategory=info, Stdoutlog4j.rootlogger=info, stdout### stdout # # #log4j. appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.target=system.outlog4j.appender.stdout.layout= Org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d{absolute}%5p-%m%n### set Package # # #log4j. logger.org.springframework=infolog4j.logger.org.apache.catalina= infolog4j.logger.org.apache.commons.digester.digester=infolog4j.logger.org.apache.catalina.startup.tldconfig= Infolog4j.logger.chb.test=debug

3 Writing data source classes

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; @Configurationpublic 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//Declare it as a Bean instance @Primary//In the same DataSource, first use the annotated DataSource public DataSource DataSource () {Druidd        Atasource 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 You can use JdbcTemplate

All of the above data sources, import data driven as needed, test using MSYQL

SpringBoot2 integration of three connection pools c3p0 Hikari Druid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.