数据库连接池的工作原理

最重要的一点就是重写connection的构造方法,使得该Connection调用close方法时并不真正的关闭,而是交还给池。


这个池如果想自己模拟可以使用LinkList来维护,创建Connection加入集合,使用时从第一个开始按顺序取出,回收的Connection也是重新加入List即可。

jdbc+c3p0:

package com.face.sql.jsu;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;

import com.mchange.v2.c3p0.ComboPooledDataSource;  

import java.beans.PropertyVetoException;  
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;  
import java.sql.SQLException;    
import java.util.Properties;
/*
 * 1,为什么使用静态代码块
 * 2,为什么使用静态方法
 * 3,为什么要按照这种顺序关闭
 */

public class JDBCUtil {
	
	public static String driver ;
    public static String url ; 
    public static String username ;  
    public static String password ;  
  
    private static final JDBCUtil instance = new JDBCUtil();  
    private static ComboPooledDataSource comboPooledDataSource;  
  
    static {  
    	InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("/db.properties");  
        Properties prop = new Properties();
        try {  
            try {  
            	prop.load(in); 
    			driver = prop.getProperty("driver");
    			url = prop.getProperty("url");
                username = prop.getProperty("username");  
                password = prop.getProperty("password");
                Class.forName(driver);  
            } catch (ClassNotFoundException e) {  
                e.printStackTrace();  
            }  catch(IOException e){
            	e.printStackTrace();
            }
            comboPooledDataSource = new ComboPooledDataSource();  
            comboPooledDataSource.setDriverClass(driver);  
            comboPooledDataSource.setJdbcUrl(url);  
            comboPooledDataSource.setUser(username);  
            comboPooledDataSource.setPassword(password);  
            //下面是设置连接池的一配置  
            comboPooledDataSource.setMaxPoolSize(20);  
            comboPooledDataSource.setMinPoolSize(5);  
        } catch (PropertyVetoException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public synchronized static Connection getConnection() {  
        Connection connection = null;  
        try {  
            connection = comboPooledDataSource.getConnection();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } finally {  
            return connection;  
        }  
    }  
  
    private JDBCUtil() {
    	
    }  
  
    public static JDBCUtil getInstance() {  
        return instance;  
    }
	
    /*
	 * 关闭连接,按照结果集,stmt,con的顺序关闭
	 */
	public static void CloseConnection(ResultSet rs, PreparedStatement pstmt, Connection con){
		try {
			if(rs != null){
			rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(pstmt != null){
			try{
				pstmt.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(con != null){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
   /*
    * 未使用连接池的方法
	static String driver;
	static String url;
	static String username;
	static String password;
	static{
		
		 // 使用静态代码块可以在启动的时候运行
		InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("/db.properties");  
        Properties prop = new Properties();
		try {
			prop.load(in); 
			driver = prop.getProperty("driver");
			url = prop.getProperty("url");
            username = prop.getProperty("username");  
            password = prop.getProperty("password");
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	//提供一个连接的方法
	public static Connection getConnection(){
		Connection conn = null;
		try {
			//连接指定数据库,数据库地址,账号,密码
			conn = DriverManager.getConnection(url+"?useUnicode=true&characterEncoding=utf-8",username,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
    */
}

你可能感兴趣的:(java)