• C3P0数据库连接池的使用


    多种开源的数据库连接池

         JDBC数据库连接池使用javax.DataSource表示,DataSource是一个接口,

          该接口通常有服务器提供实现,也有一些开源组织提供实现

                  DBCPApache提供的一个数据库连接池,速度较慢,稳定性还可以,相对C3P0较快,存在bug

                  C3P0,Hibernate 官方推荐使用,速度相对较慢,稳定性还可以

                  Druid  阿里提供的数据库连接池,集以上连接池优点于一身开发使用此连接池

    C3P0数据库连接池的两种实现方式

    TestC3P0

    package com.aff.connection;
    import java.sql.Connection;
    import java.sql.SQLException;
    import org.junit.Test;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public class TestC3P0 {
        // 方式一:
        @Test
        public void testGetConnection() throws Exception {
            // 获取c3p0数据库连接池
            ComboPooledDataSource cpds = new ComboPooledDataSource();
            cpds.setDriverClass("com.mysql.jdbc.Driver"); // loads the jdbc driver
            cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            cpds.setUser("root");
            cpds.setPassword("123456");
            // 通过设置相关的参数,对数据库连接池进行管理
            // 设置初始时数据库连接池的连接数
            cpds.setInitialPoolSize(10);
            Connection conn = cpds.getConnection();
            System.out.println(conn);
    
            // 关闭数据库连接池,一般情况下不会用
            // DataSources.destroy(cpds);
        }
    
        // 方式二:使用配置文件
        @Test
        public void testGetConnection2() throws SQLException {
            ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
            Connection conn = cpds.getConnection();
            System.out.println(conn);
    
        }
    }

    C3P0数据库连接池的配置文件

    c3p0-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
    
        <named-config name="helloc3p0">
            <!--提供获取连接的四个基本信息 -->
            <!--连接本地主机的话: jbdc:mysql://localhost:3306/test 可写成jbdc:mysql:///test -->
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
            <property name="user">root</property>
            <property name="password">123456</property>
    
    
            <!-- 对数据库连接池管理的基本信息 -->
            <!-- 当数据库连接池中的连接数不够时,c3p0一次向数据库服务器申请的连接数 -->
            <property name="acquireIncrement">5</property>
            <!-- 初始化时的连接数 -->
            <property name="initialPoolSize">10</property>
            <!-- 维护的最少连接数 -->
            <property name="minPoolSize">10</property>
            <!-- 维护的最多的连接数 -->
            <property name="maxPoolSize">100</property>
            <!-- 最多维护的Satement的个数 -->
            <property name="maxStatements">50</property>
            <!-- 每个连接最多使用Statement的个数 -->
            <property name="maxStatementsPerConnection">2</property>
    
        </named-config>
    </c3p0-config>
        

    使用C3P0数据库连接池获取连接

    JDBCUtilsC3P0

    package com.aff.util;
    import java.sql.Connection;
    import java.sql.SQLException;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    //使用C3P0数据库连接池
    public class JDBCUtilsC3P0 {
        // 把池子拿到外边,连接池一个就够,需要的连接从池子中拿
        private static ComboPooledDataSource cbpds = new ComboPooledDataSource("helloc3p0");
    
        public static Connection getConnection() throws SQLException {
            Connection conn = cbpds.getConnection();
            return conn;
        }
    }

    测试C3P0数据库连接池获取的连接的使用

        @Test
        public void testGetAll() {
            Connection conn = null;
            try {
                conn = JDBCUtilsC3P0.getConnection();
                List<Customer> list = dao.getAll(conn);
                list.forEach(System.out::println);
                System.out.println("获取成功");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.closeResource(conn, null);
            }
        }

    目录结构

    All that work will definitely pay off
  • 相关阅读:
    CocosCreator 快速开发推箱子游戏,附代码
    Cocos Creator实现左右跳游戏,提供完整游戏代码工程
    Cocos Creator一步一步实现重力球游戏,附完整代码
    PS_制作粉笔字
    高效实用的抠图方法
    PS文件和AI文件之间如何保持分层相互导入?
    行内元素之间出现间隙的bug解决
    CSS中让图片垂直居中的方法
    html中input按钮怎么添加超链接
    【转】Profiler使用方法
  • 原文地址:https://www.cnblogs.com/afangfang/p/12683932.html
Copyright © 2020-2023  润新知