码迷,mamicode.com
首页 > 其他好文 > 详细

c3p0的使用

时间:2019-12-03 01:35:19      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:def   artifact   nbsp   mysq   dep   driver   host   log   coding   

pom.xml

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.4</version>
        </dependency>

c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <!--默认配置-->
    <default-config>
        <property name="initialPoolSize">10</property>
        <property name="maxIdleTime">30</property>
        <property name="maxPoolSize">100</property>
        <property name="minPoolSize">10</property>
        <property name="maxStatements">200</property>
    </default-config>

    <!--配置连接池mysql-->
    <named-config name="mysql">
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/library_ssm?serverTimezone=GMT%2B8</property>
        <property name="user">root</property>
        <property name="password">000000</property>
        <property name="initialPoolSize">10</property>
        <property name="maxIdleTime">30</property>
        <property name="maxPoolSize">100</property>
        <property name="minPoolSize">10</property>
        <property name="maxStatements">200</property>
    </named-config>

</c3p0-config>

C3p0Util

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.log4j.Logger;

import java.sql.Connection;

public class C3p0Util {
    private static Logger logger = Logger.getLogger(C3p0Util.class);

    private static ComboPooledDataSource cpds = new ComboPooledDataSource("mysql");

    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= cpds.getConnection();
            logger.debug("从连接池获取了连接");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取连接异常");
        }
        return conn;
    }

    public static void releaseConnection(Connection conn){
        try{
            if(conn!=null){
                conn.close();
                logger.debug("释放了一个连接");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("释放连接异常");
        }
    }



}

技术图片

 

 

 

 

c3p0的使用

标签:def   artifact   nbsp   mysq   dep   driver   host   log   coding   

原文地址:https://www.cnblogs.com/Lorentz-Z/p/11974283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!