c3p0 JdbcUtilsPool

package com.ydtech.common.db;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.*;
import java.util.concurrent.CountDownLatch;

/**
 * 2008-12-6
 *
 * @author <a href="mailto:liyongibm@hotmail.com">����</a>
 */
public final class JdbcUtilsPool {
//    private String url = "jdbc:mysql://localhost:3306/test";
//    private String user = "root";
//    private String password = "root";
//    static {
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//        } catch (ClassNotFoundException e) {
//            throw new ExceptionInInitializerError(e);
//        }
//    }


    private static final ComboPooledDataSource dataSource;

    static {
        dataSource = new ComboPooledDataSource();
    }

    // private static JdbcUtilsSing instance = new JdbcUtilsSing();
    private static JdbcUtilsPool instance = null;

    private JdbcUtilsPool() {
    }

    public static JdbcUtilsPool getInstance() {
        if (instance == null) {
            synchronized (JdbcUtilsPool.class) {
                if (instance == null) {
                    instance = new JdbcUtilsPool();
                }
            }
        }
        return instance;
    }



    public Connection getConnection() throws SQLException {
        //return DriverManager.getConnection(url, user, password);
        return dataSource.getConnection();
    }

    public void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (st != null)
                    st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (conn != null)
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
            }
        }
    }


    public static void main(String[] args) throws SQLException, InterruptedException {
        long begin = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(20000);//几个工人的协作
        for (int i = 0; i < 20000; i++) {
            Worker worker1 = new Worker("zhang88" + i, latch);
            worker1.start();
        }
        latch.await();//等待所有工人完成工作
        long end = System.currentTimeMillis();

        System.out.println("Total Time: " + (end - begin) + " ms");
    }

    static class Worker extends Thread {
        String workerName;
        CountDownLatch latch;

        public Worker(String workerName, CountDownLatch latch) {
            this.workerName = workerName;
            this.latch = latch;
        }

        public void run() {
            try {
                save();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                latch.countDown();//工人完成工作,计数器减一
            }
        }
    }

    static int i=9000;
    public static void save() throws SQLException {
        Connection conn = null;
        PreparedStatement ps=null;

        try {
            conn = JdbcUtilsPool.getInstance().getConnection();
            conn.setAutoCommit(false);
            ps = conn.prepareStatement("insert into user(pwd,name)values(" + "'l'" + ",'xxx1')");
            int rs = ps.executeUpdate();
            //i++会出现重复,如果id为主键正好可以验正2个表插入的数据是否一致
            ps = conn.prepareStatement("insert into teacher(id,name,age)values(" + (i++) + ",'gg',23)");
            rs = ps.executeUpdate();
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            conn.rollback();
        }finally {
            JdbcUtilsPool.getInstance().free(null, ps, conn);
        }
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
    <!--  这是默认配置信息      -->

    <default-config>
        <!--  连接四大参数配置          -->
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <!--  池参数配置         -->
        <property name="acquireIncrement">3</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">2</property>
        <property name="maxPoolSize">100</property>
    </default-config>
    <!--  专门为oracle提供的配置信息    -->

    <named-config name="oracle-config">
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">123</property>
        <property name="acquireIncrement">3</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">2</property>
        <property name="maxPoolSize">10</property>
    </named-config>
</c3p0-config>
原文地址:https://www.cnblogs.com/yasepix/p/6420753.html