c3p0

首先到http://sourceforge.net/projects/c3p0/下载相应的jar包:



 spring-c3p0.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://172.19.59.48:3306,172.19.59.50:3306/mysql?autoReconnect=true" />
		<property name="user" value="root" />
		<property name="password" value="123456" />
		<property name="initialPoolSize" value="5" />
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="10" />
		<property name="maxIdleTime" value="60" />
		<property name="maxStatements" value="50" />
	</bean>
</beans>

连接池代码:

import java.beans.PropertyVetoException;
import java.sql.Connection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * C3P0连接池
 * 
 */
public class ConnectManager4C3P0BySpring {
	
	//使用单例模式创建数据库连接池
	private static ConnectManager4C3P0BySpring instance;
	private static ComboPooledDataSource dataSource;
	
	@SuppressWarnings("resource")
	private ConnectManager4C3P0BySpring() throws PropertyVetoException{
		ApplicationContext ac = new ClassPathXmlApplicationContext("mysql/spring-c3p0.xml");
		dataSource = (ComboPooledDataSource) ac.getBean("dataSource");
	}
	
	/**
	 * 获取数据库连接池实例(单例)
	 * @return
	 */
	public static final ConnectManager4C3P0BySpring getInstance(){
		if(instance == null){
			try{
				instance = new ConnectManager4C3P0BySpring();
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return instance;
	}
	
	/**
	 * 获取连接对象
	 * @return
	 */
	public synchronized final Connection getConnection() {
		Connection conn = null; 
		try{
			conn = dataSource.getConnection();
		} catch (Exception e){
			e.printStackTrace();
		}
		return conn;
	}

}

 测试代码:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestC3P0ConnPool {

	public static void main(String[] args) {
		System.out.println("使用数据库连接池.............");
		for(int i=0; i<10; i++){
			long beginTime = System.currentTimeMillis();			
			Connection conn = ConnectManager4C3P0BySpring.getInstance().getConnection();
			try {
				String sql = "select * from student";
				PreparedStatement stmt = conn.prepareStatement(sql);
				ResultSet rs = stmt.executeQuery(sql);
				while (rs.next()) {  
                    // do nothing...  
               } 
			} catch (Exception e){
				e.printStackTrace();
			} finally {  
                try {  
                    conn.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                }
			}
			
			long endTime = System.currentTimeMillis();  
            System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime)); 
		}
	}

}

猜你喜欢

转载自guwq2014.iteye.com/blog/2393028