2.C3P0使用
①导入c3p0-0.9.5.1.jar和mchange-commons-java-0.2.10.jar
②配置文件为c3p0-config.xml,内容如下:
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/jdbc root 123456 30000 10 30 100 10 200 10 1 0 50 100 50 1000 0 5 1 1 1 5 50
③编写工具类,代码如下:
package com.cream.ice.jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * C3P0工具类 * DBCP使用动态代理,当Connection对象调用close()方法时,将Connection对象放回连接池中,实际上并不关闭连接 * 通过c3p0-config.xml文件配置数据库、连接池参数 * @author ice * */ public class C3P0Utils { /** * 数据源 */ public static ComboPooledDataSource cpDataDataSource = new ComboPooledDataSource(); /** * 获取数据源 * @return 数据源 */ public static DataSource getDataSource() { return cpDataDataSource; } /** * 从连接池中获取连接 * @return */ public static Connection getConnection(){ try { return cpDataDataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 释放资源 */ public static void releaseResources(ResultSet resultSet, Statement statement, Connection connection) { try { if (resultSet != null) resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } finally { resultSet = null; try { if (statement != null) statement.close(); } catch (SQLException e) { e.printStackTrace(); } finally { statement = null; try { if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } finally { connection = null; } } } } }
DBCPUtils和C3P0Utils与JdbcUtils的用法别无二致,区别只是释放资源时,Connection对象调用close()方法时,只是将Connection对象放回连接池中,实际上并不关闭连接。