day48-JDBC和连接池04( 二 )


【day48-JDBC和连接池04】

day48-JDBC和连接池04

文章插图
day48-JDBC和连接池04

文章插图
package li.jdbc.datasource;import com.mchange.v2.c3p0.ComboPooledDataSource;import org.junit.Test;import java.io.FileInputStream;import java.sql.Connection;import java.util.Properties;/** * 演示c3p0的使用 */public class C3P0_ {//方式1:相关参数在程序中指定,user,url,password等@Testpublic void testC3P0_01() throws Exception {//1.创建一个数据源对象ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();//2.通过配置文件mysql.properties获取相关的连接信息Properties properties = new Properties();properties.load(new FileInputStream("src\\mysql.properties"));//读取相关的属性值String user = properties.getProperty("user");String password = properties.getProperty("password");String url = properties.getProperty("url");String driver = properties.getProperty("driver");//给数据源 comboPooledDataSource设置相关的参数//注意:连接管理 是由comboPooledDataSource来管理comboPooledDataSource.setDriverClass(driver);comboPooledDataSource.setJdbcUrl(url);comboPooledDataSource.setUser(user);comboPooledDataSource.setPassword(password);//设置初始化连接数comboPooledDataSource.setInitialPoolSize(10);//最大连接数--连接请求超过最大连接数据将进入等待队列comboPooledDataSource.setMaxPoolSize(50);//测试连接池的效率long start = System.currentTimeMillis();for (int i = 0; i < 5000; i++) {Connection connection = comboPooledDataSource.getConnection();//这个方法就是从DataSource 接口实现的//System.out.println("连接成功");connection.close();}long end = System.currentTimeMillis();System.out.println("c3p0 5000次连接mysql 耗时=" + (end - start));}}c3p0方式一:5000次的连接耗时553ms
day48-JDBC和连接池04

文章插图
10.3.2方式2-使用配置文件模板来完成首先如10.3.1一样将jar包加入到项目中
然后将c3p0提供的配置文件c3p0-config.xml复制到src目录下,该文件指定了连接数据库和连接池的相关参数
c3p0-config.xml:
<c3p0-config><!--数据源的名称,代表连接池,名字是随意的--><named-config name="hello"><!-- 驱动类 --><property name="driverClass">com.mysql.jdbc.Driver</property><!-- url--><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/hsp_db02</property><!-- 用户名 --><property name="user">root</property><!-- 密码 --><property name="password">123456</property><!-- 每次增长的连接数--><property name="acquireIncrement">5</property><!-- 初始的连接数 --><property name="initialPoolSize">10</property><!-- 最小连接数 --><property name="minPoolSize">5</property><!-- 最大连接数 --><property name="maxPoolSize">50</property><!-- 可连接的最多的命令对象数 --><property name="maxStatements">5</property><!-- 每个连接对象可连接的最多的命令对象数 --><property name="maxStatementsPerConnection">2</property></named-config></c3p0-config>测试程序:
package li.jdbc.datasource;import com.mchange.v2.c3p0.ComboPooledDataSource;import org.junit.Test;import java.sql.Connection;/** * 演示c3p0的使用 */public class C3P0_ {//方式2:使用配置文件模板来完成//将c3p0提供的配置文件c3p0-config.xml复制到src目录下// 该文件指定了连接数据库和连接池的相关参数@Testpublic void testC3P0_02() throws Exception {ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("hello");//测试5000次连接诶mysqllong start = System.currentTimeMillis();System.out.println("开始执行...");for (int i = 0; i < 5000; i++) {Connection connection = comboPooledDataSource.getConnection();//System.out.println("连接成功");connection.close();}long end = System.currentTimeMillis();System.out.println("c3p0的第二种方式 5000次连接mysql 耗时=" + (end - start));}}

经验总结扩展阅读