读书人

C3P0配备与使用以及使用前后的比较

发布时间: 2012-11-17 11:14:15 作者: rapoo

C3P0配置与使用以及使用前后的比较

一、C3P0配置

1、使用xml方式(名称为c3p0-config.xml)

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

<default-config>

<property name="jdbcUrl">jdbc:mysql://172.26.3.250:3306/csms?useUnicode=true&characterEncoding=utf-8

</property>

<property name="driverClass">com.mysql.jdbc.Driver</property>

<property name="user">root</property>

<property name="password">root</property>

?

<property name="checkoutTimeout">30000</property>

<property name="idleConnectionTestPeriod">30</property>

<property name="initialPoolSize">10</property>

<property name="maxIdleTime">30</property>

<property name="maxPoolSize">100</property>

<property name="minPoolSize">10</property>

<property name="maxStatements">200</property>

</default-config>

</c3p0-config>

2、使用properties方式(名称为c3p0.properties)

c3p0.jdbcUrl=jdbc:mysql://172.26.3.250:3306/csms?useUnicode=true&characterEncoding=utf-8

c3p0.driverClass=com.mysql.jdbc.Driver

c3p0.user=root

c3p0.password=root

?

#连接池中保留的最小连接数

c3p0.minPoolSize=5

#连接池中保留的最大连接数。Default: 15?

c3p0.maxPoolSize=30

#初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3?

c3p0.initialPoolSize=10

#最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0?

c3p0.maxIdleTime=60

#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3?

c3p0.acquireIncrement=5

#JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements

#属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。

#如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->

c3p0.maxStatements=0

#每60秒检查所有连接池中的空闲连接。Default: 0 -->

c3p0.idleConnectionTestPeriod=60

#定义在从数据库获取新连接失败后重复尝试的次数。Default: 30?

c3p0.acquireRetryAttempts=30

#获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效

#保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试

#获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->

c3p0.breakAfterAcquireFailure=true

#因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的

#时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable

#等方法来提升连接测试的性能。Default: false -->

c3p0.testConnectionOnCheckout=false

?

使用这两种方式进行配置时,只要将配置好的文件放入classpath文件夹下即可,在java代码当中不用显示的给出访问配置方式的代码,c3p0会自动识别!

二、C3P0的使用

C3P0Utils.java:

package com.c3p0.util;

?

import java.sql.Connection;

import java.sql.SQLException;

?

import javax.sql.DataSource;

?

import com.mchange.v2.c3p0.ComboPooledDataSource;

?

public class C3P0Utils {

private static DataSource ds;

?

private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); // map

static {

ds = new ComboPooledDataSource();//直接使用即可,不用显示的配置,其会自动识别配置文件

}

?

public static DataSource getDataSource() {

return ds;

}

?

public static Connection getConnection() throws SQLException {

try {

// 得到当前线程上绑定的连接

Connection conn = tl.get();

if (conn == null) { // 代表线程上没有绑定连接

conn = ds.getConnection();

tl.set(conn);

}

return conn;

} catch (Exception e) {

throw new RuntimeException(e);

}

}

?

public static void startTransaction() {

try {

// 得到当前线程上绑定连接开启事务

Connection conn = tl.get();

if (conn == null) { // 代表线程上没有绑定连接

conn = ds.getConnection();

tl.set(conn);

}

conn.setAutoCommit(false);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

?

public static void commitTransaction() {

try {

Connection conn = tl.get();

if (conn != null) {

conn.commit();

}

} catch (Exception e) {

throw new RuntimeException(e);

}

}

?

public static void closeConnection() {

try {

Connection conn = tl.get();

if (conn != null) {

conn.close();

}

} catch (Exception e) {

throw new RuntimeException(e);

} finally {

tl.remove(); // 千万注意,解除当前线程上绑定的链接(从threadlocal容器中移除对应当前线程的链接)

}

}

?

}


三、C3P0使用前后的比较

ConnectionDemo.java:

package com.c3p0;

?

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

?

import com.c3p0.util.C3P0Utils;

?

public class ConnectionDemo {

public static void main(String[] args) throws SQLException { ?

? ? ? ?System.out.println("使用连接池................................"); ?

? ? ? ?for (int i = 0; i < 20; i++) { ?

? ? ? ? ? ?long beginTime = System.currentTimeMillis(); ?

? ? ? ? ? ?Connection conn = C3P0Utils.getConnection();

? ? ? ? ? ?try { ?

? ? ? ? ? ? ? ?PreparedStatement pstmt = conn.prepareStatement("SELECT count(*) as a FROM cs_ms_common_questions"); ?

? ? ? ? ? ? ? ?ResultSet rs = pstmt.executeQuery(); ?

? ? ? ? ? ? ? ?while (rs.next()) { ?

? ? ? ? ? ? ? ?//TODO

? ? ? ? ? ? ? ?} ?

? ? ? ? ? ?} catch (SQLException e) { ?

? ? ? ? ? ? ? ?e.printStackTrace(); ?

? ? ? ? ? ?} finally { ?

? ? ? ? ? ? ? ?C3P0Utils.closeConnection(); ?

? ? ? ? ? ?} ?

? ? ? ? ? ?long endTime = System.currentTimeMillis(); ?

? ? ? ? ? ?System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime)); ?

? ? ? ?} ?

?

? ? ? ?System.out.println("不使用连接池................................"); ?

? ? ? ?for (int i = 0; i < 20; i++) { ?

? ? ? ? ? ?long beginTime = System.currentTimeMillis(); ?

? ? ? ? ? ?

? ? ? ? ? ?try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e1) {

e1.printStackTrace();

}

? ? ? ? ? ?Connection conn = DriverManager.getConnection("jdbc:mysql://172.26.3.250:3306/csms?useUnicode=true&characterEncoding=utf-8", "root","root");

? ? ? ? ? ?

? ? ? ? ? ?try { ?

? ? ? ? ? ? ? ?PreparedStatement pstmt = conn.prepareStatement("SELECT count(*) as a FROM cs_ms_common_questions"); ?

? ? ? ? ? ? ? ?ResultSet rs = pstmt.executeQuery(); ?

? ? ? ? ? ? ? ?while (rs.next()) { ?

? ? ? ? ? ? ? ?//TODO

? ? ? ? ? ? ? ?} ?

? ? ? ? ? ?} catch (SQLException e) { ?

? ? ? ? ? ? ? ?e.printStackTrace(); ?

? ? ? ? ? ?} finally { ?

? ? ? ? ? ? ? ?try { ?

? ? ? ? ? ? ? ? ? ?conn.close(); ?

? ? ? ? ? ? ? ?} catch (SQLException e) { ?

? ? ? ? ? ? ? ? ? ?e.printStackTrace(); ?

? ? ? ? ? ? ? ?} ?

? ? ? ? ? ?} ?

? ? ? ? ? ?long endTime = System.currentTimeMillis(); ?

? ? ? ? ? ?System.out.println("第" + (i + 1) + "次执行花费时间为:" ?+ (endTime - beginTime)); ?

? ? ? ?} ?

?

? ?} ?

}


最后是控制台输出:使用连接池................................第1次执行花费时间为:453第2次执行花费时间为:16第3次执行花费时间为:0第4次执行花费时间为:15第5次执行花费时间为:16第6次执行花费时间为:15第7次执行花费时间为:16第8次执行花费时间为:0第9次执行花费时间为:0第10次执行花费时间为:0第11次执行花费时间为:0第12次执行花费时间为:0第13次执行花费时间为:0第14次执行花费时间为:0第15次执行花费时间为:0第16次执行花费时间为:16第17次执行花费时间为:0第18次执行花费时间为:0第19次执行花费时间为:0第20次执行花费时间为:0不使用连接池................................第1次执行花费时间为:0第2次执行花费时间为:15第3次执行花费时间为:16第4次执行花费时间为:0第5次执行花费时间为:16第6次执行花费时间为:0第7次执行花费时间为:15第8次执行花费时间为:16第9次执行花费时间为:0第10次执行花费时间为:15第11次执行花费时间为:0第12次执行花费时间为:16第13次执行花费时间为:0第14次执行花费时间为:16第15次执行花费时间为:15第16次执行花费时间为:0第17次执行花费时间为:16第18次执行花费时间为:0第19次执行花费时间为:16第20次执行花费时间为:0
可以看出,在使用连接池时,第一次执行花费的时间稍长,因为第一次初始化操作需要创建多个连接并放入池中,以后使用时将会大大缩短执行时间。?

在不使用连接池时,每次花费的时间都比较长。

读书人网 >软件架构设计

热点推荐