c3p0

First go to http://sourceforge.net/projects/c3p0/ to download the corresponding jar package:



 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>

 

Connection pool code:

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 connection pool
 *
 */
public class ConnectManager4C3P0BySpring {
	
	//Create a database connection pool using the singleton pattern
	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");
	}
	
	/**
	 * Get database connection pool instance (singleton)
	 * @return
	 */
	public static final ConnectManager4C3P0BySpring getInstance(){
		if(instance == null){
			try{
				instance = new ConnectManager4C3P0BySpring();
			} catch (Exception e){
				e.printStackTrace ();
			}
		}
		return instance;
	}
	
	/**
	 * Get the connection object
	 * @return
	 */
	public synchronized final Connection getConnection() {
		Connection conn = null;
		try{
			conn = dataSource.getConnection();
		} catch (Exception e){
			e.printStackTrace ();
		}
		return conn;
	}

}

 Test code:

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("Use database connection pool............");
		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("The first" + (i + 1) + "The execution time is:" + (endTime - beginTime));
		}
	}

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326121781&siteId=291194637