手记4 - Boot连接池(默认c3p0阿里druid)

Posted 西二旗程序员

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手记4 - Boot连接池(默认c3p0阿里druid)相关的知识,希望对你有一定的参考价值。

SpringBoot连接池


Boot提供了默认内置集成的DataSource,也可以使用外部的。


1. 默认连接池

  • 默认内集成DataSource对象(默认,id名dataSource)

    开发只需要添加下面连接池jar包引入,Boot可以自动创建出DataSource连接池对象。

        <!-- tomcat连接池和jdbc支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- 驱动包 -->
        <dependency>
          <groupId>com.oracle</groupId>
          <artifactId>ojdbc6</artifactId>
          <version>11.2.0.3</version>
        </dependency>


    • 内部会优先采用tomcat-jdbc连接池;

    • 然后找HikariCP连接池;

    • 最后找dbcp、dbcp2连接池;

    • 在application.properties配置文件定义连接池参数约定如下

      spring.datasource.username=xxx
      spring.datasource.password=xxx
      spring.datasource.url=xxx
      #spring.datasource.driver-class-name=xxx
      spring.datasource.type=xxx

    • 追加spring-boot-starter-jdbc.jar内部会依赖导入tomcat-jdbc功能

    • 如果追加了dbcp连接池,默认还是找tomcat-jdbc,想要改变可以设置spring.datasource.type=包名.类名


2.自定义连接池

连接池种类很多,常用的有dbcp2、c3p0、druid、proxool等

1) 创建c3p0连接池


     - 导入c3p0连接池jar引入

            <dependency>
              <groupId>c3p0</groupId>
              <artifactId>c3p0</artifactId>
              <version>0.9.1.2</version>
            </dependency>

    - 在application.properties定义c3p0连接池参数

            spring.datasource.c3p0.user=SCOTT
            spring.datasource.c3p0.password=TIGER
            spring.datasource.c3p0.jdbcUrl=jdbc:oracle:thin:@localhost:1521/XE
            spring.datasource.c3p0.driverClass=oracle.jdbc.OracleDriver

    - 在入口类子包创建C3p0DataSourceConfig.java

            @Configuration//支持@Bean,内部带有@Component
            public class C3p0DataSourceConfig {
                
                @Bean(name="c3p0")//将返回的dataSource添加到Spring容器
                @ConfigurationProperties(prefix="spring.datasource.c3p0")
                public DataSource createC3p0(){
            //        ComboPooledDataSource ds = new ComboPooledDataSource();
                    DataSource ds = DataSourceBuilder.create()
                        .type(ComboPooledDataSource.class)
                        .build();
                    return ds;
                }
                
            }

    - 测试类

      @RunWith(SpringRunner.class)
      @SpringBootTest(classes={BootApplication.class})
      public class TestDataSource {
                
          @Resource(name="c3p0")
          private DataSource ds;
                
          @Test
          public void test1(){
              System.out.println(ds);
          }
                
       }


  2) 创建druid连接池


       - 导入druid连接池jar引入

          <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
          </dependency>

        - 在application.properties定义druid连接池参数

                spring.datasource.druid.username=SCOTT
                spring.datasource.druid.password=TIGER
                spring.datasource.druid.url=jdbc:oracle:thin:@localhost:1521/XE
                spring.datasource.druid.driverClassName=oracle.jdbc.OracleDriver


        - 在入口类子包创建DruidDataSourceConfig.java

                @Configuration
                public class DruidDataSourceConfig {
                    
                    @Bean(name="druid")
                    @Primary//将当前连接池作为默认的
                    @ConfigurationProperties(prefix="spring.datasource.druid")
                    public DataSource createDruid(){
                        DataSource ds = DataSourceBuilder.create()
                            .type(DruidDataSource.class).build();
                        return ds;
                    }
                    
                }

        - 测试类

                @RunWith(SpringRunner.class)
                @SpringBootTest(classes={BootApplication.class})
                public class TestDataSource {
                    
                    @Resource//默认注入带@Primay的连接池
                //    @Autowired
                //    @Qualifier("druid")
                    private DataSource ds;
                    
                    @Test
                    public void test1(){
                        System.out.println(ds);
                    }
                    
                }

以上是关于手记4 - Boot连接池(默认c3p0阿里druid)的主要内容,如果未能解决你的问题,请参考以下文章

spring boot整合Druid连接池

2019.4.1 数据库连接池

Boot使用C3P0连接池

spring boot整合Druid连接池

spring boot 集成Druid

c3p0连接池 & JdbcUtils