• 多个C3P0的java举例


    在使用mysql时,如果数据库会被频繁多人调用,有必要使用连接池来帮助协调,使用C3P0连接池时想要用多个数据库时,需要分别定义ComboPooledDataSource的静态对象。举例如下:

    public class ConnOfC3P0Util {
        private static Log logger = LogFactory.getLog(ConnOfC3P0Util.class);
        private static ComboPooledDataSource ds = new ComboPooledDataSource();
        private static ComboPooledDataSource ds2 = new ComboPooledDataSource("myApp"); 
        public static ComboPooledDataSource getDs() {
            return ds;
        }
    
        public static Connection getConnection() {
            try {
                logger.info("ds.getDataSourceName():"+ds.getDataSourceName());
                return ds.getConnection();
    
            } catch (SQLException e) {
                logger.error(e.getMessage(), e);
            }
            return null;
        }
        public static Connection getMyConnection() {
            try {
                logger.info("ds2.getDataSourceName():"+ds2.getDataSourceName());
                return ds2.getConnection();
    
            } catch (SQLException e) {
                logger.error(e.getMessage(), e);
            }
            return null;
        }
    
        public static void free(ResultSet rs, Statement st, Connection conn) {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (Exception e) {
                logger.error( e.getMessage(), e);
            } finally {
                try {
                    if (st != null) {
                        st.close();
                    }
                } catch (Exception e) {
                    logger.error( e.getMessage(), e);
                } finally {
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (SQLException e) {
                            logger.error(e.getMessage(), e);
                        }
                    }
                }
            }
        }
    }

    相应的c3p0xml配置为:

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
        <default-config>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="user">XXX</property>
            <property name="password">XXX</property>
            <property name="jdbcUrl">jdbc:mysql://XXX
            useUnicode=true&amp;characterEncoding=utf-8&amp;connectTimeout=60000&amp;socketTimeout=60000</property>
    
            <property name="initialPoolSize">20</property>
            <property name="maxPoolSize">50</property>
            <property name="minPoolSize">5</property>
            <property name="acquireIncrement">2</property>
            <property name="maxIdleTime">10</property>
            <property name="maxStatements">30</property>        
            <property name="idleConnectionTestPeriod">21600</property>
        </default-config>
        <named-config name="myApp">
            <property name="user">YYY</property>
            <property name="password">YYY</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://YYY</property>
    
            <property name="initialPoolSize">10</property>
            <property name="maxIdleTime">30</property>
            <property name="maxPoolSize">100</property>
            <property name="minPoolSize">10</property>
        </named-config>
    
    </c3p0-config>
    
  • 相关阅读:
    [BZOJ 1095] [ZJOI 2007]Hide 捉迷藏
    [BZOJ 2759] 一个动态树好题
    BZOJ 3122 SDOI2013 随机数生成器
    [NOIP集训]10月18日
    [NOIP集训]10月17日
    [NOIP集训]10月16日
    [NOI题库]1.3编程基础之算术表达式与顺序执行 题解(一)
    [NOI题库]1.2编程基础之变量定义、赋值及转换 题解
    [NOI题库]1.1编程基础之输入输出 题解
    [作业]排序算法练习(二)
  • 原文地址:https://www.cnblogs.com/zhangdebin/p/5567903.html
Copyright © 2020-2023  润新知