• c3p0连接池配置以及使用


    1.所需要的jar包

    https://mvnrepository.com/artifact/com.mchange/c3p0

    https://mvnrepository.com/artifact/com.mchange/mchange-commons-java/0.2.15

    2.配置c3p0-config.xml文件 放在src文件夹下

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
        <!--  默认配置  -->
        <default-config>
            <!--   连接参数  -->
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/Manager</property>
            <property name="user">root</property>
            <property name="password">root</property>
            <!--  连接池参数  -->
            <!-- 初始化申请的连接数量 -->
            <property name="initialPoolSize">5</property>
            <!-- 最大的连接数量 -->
            <property name="maxPoolSize">10</property>
            <!-- 超时时间 -->
            <property name="checkoutTimeout">3000</property>
        </default-config>
        <!--c3p0配置1-->
        <named-config name="c3p0">
            <!--   连接参数  -->
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/manager</property>
            <property name="user">root</property>
            <property name="password">root</property>
            <!--  连接池参数  -->
            <property name="initialPoolSize">5</property>
            <property name="maxPoolSize">8</property>
            <property name="checkoutTimeout">1000</property>
        </named-config>
    </c3p0-config>

    3.配置c3p0数据源工具类

    public class ComboPooledDataSourceUtil {
    private static ComboPooledDataSource dataSource =new ComboPooledDataSource("c3p0");
    //xml编码 public static DataSource getDataSourceByxml(){
    return dataSource;
    } }

    4.测试

      @Test
        //测试c3p0
        public void test_c3p0() throws PropertyVetoException, SQLException {
            Connection connection = ComboPooledDataSourceUtil.getDataSourceByxml().getConnection();
            //Connection connection = ComboPooledDataSourceUtil.getDataSource().getConnection();
            System.out.println(connection);
        }
  • 相关阅读:
    MongoDB的固定集合
    MongoDB的导入导出
    MongoDB的数据备份与恢复
    MongoDB的索引
    MongoDB简单CRUD场景
    MongoDB入门
    NOSQL概念入门
    Java静态代理和动态代理
    a=a+1背后的内存模型和CPU高速缓存
    SpringCloud的学习记录(6)
  • 原文地址:https://www.cnblogs.com/hyy9527/p/13151373.html
Copyright © 2020-2023  润新知