继上一篇介绍dbcp的配置,本文重点介绍下c3p0的配置及实例演示。
上一篇DBCP的配置可以参见:http://sjsky.iteye.com/blog/1105674
proxool的配置介绍也已经发表:http://sjsky.iteye.com/blog/1108808
本文的目录结构大致如下 :
一、参数介绍
二、结合spring、hibernate中的配置示例
[ 一 ]、参数介绍
Xml代码 收藏代码
select id from tb_c3p0_test where id=1
和hibernate配置属性的对照表:
c3p0-native property name hibernate configuration key
c3p0.acquireIncrement hibernate.c3p0.acquire_increment
c3p0.idleConnectionTestPeriod hibernate.c3p0.idle_test_period
c3p0.initialPoolSize not available -- uses minimum size
c3p0.maxIdleTime hibernate.c3p0.timeout
c3p0.maxPoolSize hibernate.c3p0.max_size
c3p0.maxStatements hibernate.c3p0.max_statements
c3p0.minPoolSize hibernate.c3p0.min_size
c3p0.testConnectionsOnCheckout
hibernate.c3p0.validate hibernate 2.x only!
[ 二 ]、spring、hibernate中的配置示例
c3p0.jdbc.properties
Java代码 收藏代码
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/iecms
jdbc.username=root
jdbc.password=
[ 1 ]、基础测试
Java代码 收藏代码
package michael.jdbc.c3p0;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import michael.jdbc.dbcp.DbcpDataSourceExample;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* @see http://sjsky.iteye.com
* @author michael [email protected]
*/
public class C3p0DataSourceExample {
/**
* @param args
*/
public static void main(String[] args) {
String testSql = "select * from TB_MYTEST";
String cfgFileName = "c3p0.jdbc.properties";
System.out.println("init c3p0 ComboPooledDataSource start ");
DataSource dataSource = initDataSource(cfgFileName);
System.out.println("ComboPooledDataSource init done.");
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
System.out.println("Creating connection start.");
conn = dataSource.getConnection();
System.out.println("Creating statement start.");
stmt = conn.createStatement();
System.out.println("Executing statement start.");
rset = stmt.executeQuery(testSql);
System.out.println("executeQuery Results:");
int numcols = rset.getMetaData().getColumnCount();
while (rset.next()) {
for (int i = 1; i <= numcols; i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
System.out.println("Results display done.");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rset != null)
rset.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @param cfgFileName
* @return DataSource
*/
public static DataSource initDataSource(String cfgFileName) {
ComboPooledDataSource ds = new ComboPooledDataSource();
try {
Properties cfgpp = new Properties();
cfgpp.load(C3p0DataSourceExample.class
.getResourceAsStream(cfgFileName));
ds.setDriverClass(cfgpp.getProperty("jdbc.driverClassName"));
ds.setJdbcUrl(cfgpp.getProperty("jdbc.url"));
ds.setUser(cfgpp.getProperty("jdbc.username"));
ds.setPassword(cfgpp.getProperty("jdbc.password"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
return ds;
}
}
运行结果
init c3p0 ComboPooledDataSource start
ComboPooledDataSource init done.
Creating connection start.
Creating statement start.
Executing statement start.
executeQuery Results:
1 batch_add_0 2011-06-16 14:29:08.0
2 batch_add_1 2011-06-16 14:29:08.0
3 batch_add_2 2011-06-16 14:29:08.0
4 batch_add_3 2011-06-16 14:29:08.0
5 batch_add_4 2011-06-16 14:29:08.0
Results display done.
[ 2 ]、结合spring的配置测试
c3p0.ds.spring.xml
Xml代码 收藏代码
classpath:michael/jdbc/c3p0/c3p0.jdbc.properties
destroy-method="close">
Java代码 收藏代码
package michael.jdbc.c3p0;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* @see http://sjsky.iteye.com
* @author michael [email protected]
*/
public class C3p0SpringExample {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("c3p0.ds.spring.xml init start ");
ApplicationContext appCt = new ClassPathXmlApplicationContext(
"michael/jdbc/c3p0/c3p0.ds.spring.xml");
System.out.println("spring bean create ComboPooledDataSource");
DataSource dataSource = (ComboPooledDataSource) appCt
.getBean("c3p0DataSource");
String testSql = "select * from TB_MYTEST";
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
System.out.println("Creating connection start.");
conn = dataSource.getConnection();
System.out.println("Creating statement start.");
stmt = conn.createStatement();
System.out.println("Executing statement start.");
rset = stmt.executeQuery(testSql);
System.out.println("executeQuery Results:");
int numcols = rset.getMetaData().getColumnCount();
while (rset.next()) {
for (int i = 1; i <= numcols; i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
System.out.println("Results display done.");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rset != null)
rset.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
运行结果:
c3p0.ds.spring.xml init start
spring bean create ComboPooledDataSource
Creating connection start.
Creating statement start.
Executing statement start.
executeQuery Results:
1 batch_add_0 2011-06-16 14:29:08.0
2 batch_add_1 2011-06-16 14:29:08.0
3 batch_add_2 2011-06-16 14:29:08.0
4 batch_add_3 2011-06-16 14:29:08.0
5 batch_add_4 2011-06-16 14:29:08.0
Results display done.
[ 3 ]、结合hibernate的配置测试
hibernate.cfg.xm
Xml代码 收藏代码
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/iecms
org.hibernate.connection.C3P0ConnectionProvider
org.hibernate.dialect.MySQL5Dialect
Java代码 收藏代码
package michael.jdbc.c3p0;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* @see http://sjsky.iteye.com
* @author michael [email protected]
*/
public class C3p0HibernateExample {
/**
* @param args
*/
public static void main(String[] args) {
SessionFactory sessionFactory = null;
try {
System.out.println("c3p0/hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/jdbc/c3p0/hibernate.cfg.xml");
System.out.println(" create sessionFactory ");
sessionFactory = config.buildSessionFactory();
System.out.println(" create session ");
Session session = sessionFactory.getCurrentSession();
String testSql = "select * from TB_MYTEST";
System.out.println(" beginTransaction ");
Transaction ta = session.beginTransaction();
org.hibernate.Query query = session.createSQLQuery(testSql);
List