zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    django1.6 django-dajaxice的安装配置.
    jquery js ajax 不错的想法
    django1.6 GET url传参 乱码
    django1.6 CSRF verification failed. Request aborted. 用出现表单提交
    Caml语句中筛选lookup字段
    sharepoint Jsom一些基本操作
    JSOM启动工作流
    数据量太大时,如何实现分页查询-CSOM
    数据量太大时,如何实现分页查询-JSOM
    JS加强学习-DOM学习02
  • 原文地址:https://www.cnblogs.com/yasepix/p/6420753.html
Copyright © 2011-2022 走看看