JDBC - 使用C3p0数据源

来源:互联网 发布:苏联核弹攻击中国 知乎 编辑:程序博客网 时间:2024/05/23 10:40

【1】XML配置

<c3p0-config>  <named-config name="mvcapp">    <property name="user">root</property>     <property name="password">123456</property>     <property name="driverClass">com.mysql.jdbc.Driver</property>     <property name="jdbcUrl">jdbc:mysql:///test</property>     <property name="acquireIncrement">5</property>    <property name="initialPoolSize">10</property>    <property name="minPoolSize">10</property>    <property name="maxPoolSize">50</property>    <!-- intergalactoApp adopts a different approach to configuring statement caching -->    <property name="maxStatements">20</property>     <property name="maxStatementsPerConnection">5</property>  </named-config></c3p0-config>

【2】初始化并获取链接

public class JdbcUtils {    /**jdbc操作的工具类     * @param args     */    /*释放connection*/    public static void releaseConnection(Connection connection){        try {            if (connection!=null) {                connection.close();                 }        } catch (Exception e) {            e.printStackTrace();        }    }    private static ComboPooledDataSource dataSource = null;    static{        /*数据源只能被创建一次*/        dataSource=new ComboPooledDataSource("mvcapp");    }    /*返回数据源的一个connection对象*/    public static Connection getConnection() throws SQLException{        return dataSource.getConnection();    }}
0 0
原创粉丝点击