心得2--自定义连接池与开源组织编写的连接池案例分析

1.自己编写的连接池案例
package com.csdn.uil;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;

import javax.sql.DataSource;

/**
 * 自己编写一个连接池,模拟dbcp、c3p0等连接池;实现了DataSourc接口
 */
public class DBPool implements DataSource {

	private static String driver;
	private static String url;
	private static String username;
	private static String password;
	private static Connection con;
    //这里用LinkedList是因为取出放回的时候要按顺序取出,顺序放回
	private static LinkedList<Connection> list = new LinkedList<Connection>();
	static {
		InputStream in = DBManager.class.getClassLoader().getResourceAsStream(
				"db.properties");
		Properties pro = new Properties();
		try {
			pro.load(in);
			driver = pro.getProperty("driver");
			url = pro.getProperty("url");
			username = pro.getProperty("username");
			password = pro.getProperty("password");
			Class.forName(driver);
			//手动设置向连接池加入10个连接
			for (int i = 0; i < 10; i++) {
				con = DriverManager.getConnection(url, username, password);
				System.out.println("向连接池中加入了" + con);
				list.add(con);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	@Override
	public Connection getConnection() throws SQLException {
		if(list.size()>0){
			//这里不用get的原因是这里取出一个连接,是取出(连接池里没有了),用get连接池里还有
			final Connection con = list.removeFirst();
			System.out.println("用户从池中取走了:"+con);
			System.out.println("池的大小为:"+list.size());
			return (Connection)Proxy.newProxyInstance(DBPool.class.getClassLoader(), new Class[]{Connection.class}, new InvocationHandler() {
				
				@Override
				public Object invoke(Object proxy, Method method, Object[] args)
						throws Throwable {
					if(!method.getName().equalsIgnoreCase("close")){
						return method.invoke(con, args);
					}
					System.out.println(con+"被放回了");
					list.add(con);
					System.out.println("池的大小为:"+list.size());
					return null;
				}
			});
		}else{
			//throw new RuntimeException("对不起,请稍等!");
			return null;
		}
	}
	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		return null;
	}
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return null;
	}
	@Override
	public int getLoginTimeout() throws SQLException {
		return 0;
	}
	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
	}
	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
	}
	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return null;
	}
}
2.Apache组织的连接池:dbcp
package com.csdn.uil;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DBManager_dbcp {
	
	private static DataSource ds;
	static{
		InputStream in = DBManager_dbcp.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
		Properties pro = new Properties();
		try {
			pro.load(in);
			BasicDataSourceFactory factory = new BasicDataSourceFactory();
			ds = factory.createDataSource(pro);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection() throws SQLException{
		return ds.getConnection();
	}
}
3. c3p0连接池
package com.csdn.uil;
import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBManager_c3p0 {
	private static ComboPooledDataSource ds = null;
	static{
		//这里要注意的是:xml配置文件的名字一定要是c3p0-config.xml,可以doc参考帮助,帮助中有命名,如果名字写错了会无法加载驱动,空指针的错
		ds = new ComboPooledDataSource("mysql");
		
		/*try {
			ds =new ComboPooledDataSource();
			ds.setDriverClass("com.mysql.jdbc.Driver");
			//自己写的时候一定要区分com.mysql.jdbc和jdbc:mysql:的顺序区别,写错之后会出无法加载driver驱动的错
			ds.setJdbcUrl("jdbc:mysql://localhost:3306/login");
			ds.setUser("root");
			ds.setPassword("root");
			
			ds.setInitialPoolSize(10);
			ds.setMaxPoolSize(20);
			ds.setMinPoolSize(5);
			
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}*/
	}
    public static Connection getCon() throws SQLException{
    	return ds.getConnection();
    }
}
   这里要注意的是:c3p0连接池需要一个xml配置文件c3p0-config.xml;而且名字是固定的,一定是c3p0-config,中连线不是下划线,错了会出错
<c3p0-config>
	<default-config>
	<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/login</property>
		<property name="user">root</property>
		<property name="password">root</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>

	<named-config name="mysql">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/login</property>
		<property name="user">root</property>
		<property name="password">root</property>
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">10</property>
		<property name="minPoolSize">50</property>
		<property name="maxPoolSize">1000</property>
	</named-config>
</c3p0-config> 


你可能感兴趣的:(java,java,java,jdbc,jdbc,c3p0,DBCP,自定义连接池)