zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    SharePoint Error occurred in deployment step 'Recycle IIS Application Pool': 0x80070005:拒绝访问
    Getting SharePoint objects (spweb, splist, splistitem) from url string
    SharePoint 2010用“localhost”方式访问网站,File not found问题处理方式
    在 SharePoint 2010 打开网页出错时,显示实际的错误信息
    解决 SharePoint 2010 拒绝访问爬网内容源错误的小技巧(禁用环回请求的两种方式)
    SQL Server 删除数据库所有表和所有存储过程
    SQL Server 查询数据库表的列数
    SQL Server 自定义字符串分割函数
    sp_configure命令开启组件Agent XPs,数据库计划(Maintenance Plan)
    SQL Server 2008 收缩日志(log)文件
  • 原文地址:https://www.cnblogs.com/afangfang/p/12683932.html
Copyright © 2011-2022 走看看