Java测试开发--MySql之C3P0连接池(八)

连接池C3P0!

连接池技术的目的:解决建立数据库连接耗费资源和时间很多的问题,提高性能 !

下面以案例演示下C3P0的操作流程.

1、测试准备:

①MySql数据库一枚
②database名为mytest


③项目工程

 

 


2、代码展示

pom.xml的部分代码

 <dependency>
    <groupId>commons-dbutils</groupId>
    <artifactId>commons-dbutils</artifactId>
    <version>1.7</version>
</dependency>

<!-- 连接池 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.4</version>
</dependency>

 

 

c3p0-config.xml

<named-config name="mysql2">
        <!-- 配置JDBC 四个基本属性 -->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://1.11.85.XX:3307/mytest?characterEncoding=utf8&amp;useSSL=false
        </property>
        <property name="user">root</property>
        <property name="password">123456</property>
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement">3</property>
        <!-- 初始化数据库连接池时连接的数量 -->
        <property name="initialPoolSize">5</property>
        <!-- 数据库连接池中的最小的数据库连接数 -->
        <property name="minPoolSize">2</property>
        <!-- 数据库连接池中的最大的数据库连接数 -->
        <property name="maxPoolSize">10</property>
    </named-config>

 

 

JdbcUtils.java

package apitest.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtils {
    public static final  ComboPooledDataSource ds_deault = new ComboPooledDataSource(); //统一数据连接关联
    
    public static DataSource getDataSource(String type) {
        if("mysql".equals(type)) {
            return ds_mysql;
        }else if ("mysql2".equals(type)) {
            return ds_mysql2;
        }
        
        return ds_deault;
        
    }
    
    public static DataSource getDataSource() {
        return getDataSource("");
    }
    
    public static Connection getConnection(String type) throws SQLException {
        return getDataSource(type).getConnection();
    }
    
    public static Connection getConnection() throws SQLException {
        return getDataSource("").getConnection();
    }
}

 

public class DbCheckUtils {
    
    public  static boolean dbCheck(String dbCheck) throws SQLException {
        if(StringUtils.isNotBlank(dbCheck)) {
          String[] dbcheck_array=dbCheck.split(",");
          String sql=dbcheck_array[0];
          sql=ParamsUtils.replace(sql);
          QueryRunner runner=null;
          if(dbcheck_array.length<=2) {
               runner = new QueryRunner(JdbcUtils.getDataSource());
          }else {
               runner = new QueryRunner(JdbcUtils.getDataSource(dbcheck_array[2])); 
          }
         
           System.out.println("sql "+sql);
           List<Map<String, Object>> list =  runner.query(sql, new MapListHandler());
           JsonCheckResult jsonrResult= CheckPointUtils.check(JSON.toJSONString(list), dbcheck_array[1]);
           return jsonrResult.isResult();
        }
        return false;
    }
View Code

 

posted @ 2021-08-26 14:27  胖儿1018  阅读(104)  评论(0编辑  收藏  举报