java数据库连接池代码实现

连接池原理已经解释了,下面该看看代码是如何实现的吧

    数据库连接池有很多,我就不一一列举了,有 dbcp,c3p0, Proxool,BoneCP,Druid............强迫症可以自己去百度,然后背下来

简单介绍一下,

DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要2个包:commons-dbcp.jar,commons-pool.jar

C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。mysql-connector-java-5.0.8-bin.jar,mchange-commons-0.2.jar,c3p0-0.9.2-pre1.jar(版本自己自由发挥)

Proxool是一种Java数据库连接池技术。sourceforge下的一个开源项目,这个项目提供一个健壮、易用的连接池,最为关键的是这个连接池提供监控的功能,方便易用,便于发现连接泄漏的情况 。commons-logging.jar,proxool-0.9.0RC2.jar

  好,常见的就以上三种,其它就不介绍了,可以自己百度


最精彩的代码片段来了

import java.io.InputStream;
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 org.apache.commons.dbcp.BasicDataSourceFactory;



public class JdbcUtilDBCP {
	
	private static DataSource datasource = null;
	static{
		try{
		//读取资源文件中的信息
		InputStream in = JdbcUtilDBCP.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
		Properties config = new Properties();
		config.load(in);
		BasicDataSourceFactory factory = new BasicDataSourceFactory();
		datasource = factory.createDataSource(config);
		}catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	public static Connection getConnection() throws SQLException{
		return datasource.getConnection();
	}
	
	public static void release(Connection conn, Statement st, ResultSet rs){
		if (rs != null){
			try{
				rs.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			rs = null;
		}
		if (st != null){
			try{
				st.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			st = null;
		}
		if (conn != null){
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
}
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test1
username=root
password=123456
#
   
   
initialSize=10
#最大连接数量
maxActive=50
#
   
   
maxIdle=20
#
   
   
minIdle=5
#
   
   
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;] 
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=utf8
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_COMMITTED

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


import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtilC3P0 {
	private static ComboPooledDataSource ds = null;
	
	static{
		try{
			ds = new ComboPooledDataSource();
		}catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	public static Connection getConnection() throws SQLException{
		return ds.getConnection();
	}
	
	
	public static void release(Connection conn,Statement st,ResultSet rs){
		
		if(rs!=null){
			try{
				rs.close();   //throw new 
			}catch (Exception e) {
				e.printStackTrace();
			}
			rs = null;
		}
		if(st!=null){
			try{
				st.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			st = null;
		}
		if(conn!=null){
			try{
				conn.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
}

    
    
	
     
     
		
      
      
       
       com.mysql.jdbc.Driver
      
      
		
      
      
       
       jdbc:mysql://localhost:3306/user
      
      
		
      
      
       
       root
      
      
		
      
      
       
       root
      
      
		
		
      
      
       
       10
      
      
		
      
      
       
       30
      
      
		
      
      
       
       20
      
      
		
      
      
       
       5
      
      
		
      
      
       
       200
      
      
	
     
     
	
	
     
     
		
      
      
       
       50
      
      
		
      
      
       
       100
      
      
		
      
      
       
       50
      
      
		
      
      
       
       1000
      
      
      
      
		
      
      
       
       0
      
      
		
      
      
       
       5
      
      
	
     
     

    
    

    
    
	
     
     
		
      
      
       
       com.mysql.jdbc.Driver
      
      
		
      
      
       
       jdbc:mysql://localhost:3306/user
      
      
		
      
      
       
       root
      
      
		
      
      
       
       root
      
      
		
		
      
      
       
       10
      
      
		
      
      
       
       30
      
      
		
      
      
       
       20
      
      
		
      
      
       
       5
      
      
		
      
      
       
       200
      
      
	
     
     
	
	
     
     
		
      
      
       
       50
      
      
		
      
      
       
       100
      
      
		
      
      
       
       50
      
      
		
      
      
       
       1000
      
      
      
      
		
      
      
       
       0
      
      
		
      
      
       
       5
      
      
	
     
     

    
    






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斗码士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值