超级简单的数据库连接池(支持多数据源)

news/2024/5/19 2:10:09 标签: 数据库连接池, sqlserver, null, jdbc, sql, object
连接池最基本的目的: 
1
、重用连接,节省连接资源; 
2
、免去建立连接操作,提高效率
3. 
限制最大连接并发数


自己弄的一个连接池,绝对高效安全,支持多数据源


连接池的两种实现方式:

1. 
修饰模式弄个ConnectionWrapper类出来 
2. 
动态代理


这里采用的是第一种方式



ConnectionAdapter  implements Connetion
ConnectionWrapper extends ConnectionAdapter 

ConnectionWrapper 
中封装了connection对象
close
方法重写为
{
 SimpleConnectionPool.putConnectionToPool(this);
 //
把连接返回连接池
}


配置文件 app.properties


ConnectionAdapter 
DBCP拷贝过来,删了些垃圾东西

支持多数据源

DBUtil.getConn();//
获取默认连接池
DBUtil.getConn(pool_name);//
获取指定名称的连接池



各参数说明

pool_name.dirver

driver=net.sourceforge.jtds.jdbc.Driver
url=jdbc:jtds:sqlserver>sqlserver://127.0.0.1:1433/data
user=sa
pwd=sa
type=sqlserver>sqlserver //
数据库类型
pool=1        //
是否使用连接池 1 使用
max_wait=3    //
连接池已满时,最大等待时间 ,单位 s
#timeout ,wait time when max active ,  unit s
max_active=20  //
最大活动连接数,用这个来控制最大并发数
min_free=3     //
连接池中最小空闲连接数
max_free=20    //
连接池中最大空闲连接数,默认=max_active
check_sql=select getdate()  //
验证连接是否有效,
clear_time=2                //
清理空闲连接
#clear_time,remove the old connection,unit min




还可以用动态代理的方式实现连接池


2.

动态代理实现



class ConnectionWrapper implements InvocationHandler {

private final static String CLOSE_METHOD_NAME = "close";
  public Connection connection = null;
  private Connection m_originConnection = null;

 ConnectionWrapper(Connection con,String pool_name)throws Exception {
    this.pool_name=pool_name;
    
    this.check_sql = getCheckSqlByPoolName(pool_name);
    
    last_use_time = System.currentTimeMillis();
    create = StringUtil.getNowTime()+"";
    
      Class[] interfaces = {java.sql.Connection.class};
    this.connection = (Connection) Proxy.newProxyInstance(
      con.getClass().getClassLoader(),
      interfaces, this);
    m_originConnection = con;
  }

  
  void close() {
    //m_originConnection.close();
      DBUtil.close(m_originConnection);
  }

  public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
    Object obj = null;
    if (CLOSE_METHOD_NAME.equals(m.getName())) {
        //SimpleConnectionPool.pushConnectionBackToPool(this);
        SimpleConnectionPool.putConnectionToPool(this);
        
    }
    else {
        
      obj = m.invoke(m_originConnection, args);
      //last_use_time = System.currentTimeMillis();
    }
  
    
    return obj;
  }
  

}


              
代码不全,只提供连接池部分代码,学习交流用



      enjoy
                               giscat
                                        2006-11-15 附件:pool.rar(9K)  
 

http://www.niftyadmin.cn/n/610982.html

相关文章

bilibiliC++63指针-指针配合数组和函数案例

7.8 指针、数组、函数 案例描述: 封装一个函数,利用冒泡排序,实现对整型数组的升序排序 例如数组:int arr[10] { 4,3,6,9,1,2,10,8,7,5 }; 示例: //冒泡排序函数 void bubbleSort(int * arr, int len) //int * arr…

构建RESTful Web服务

加入现在我们想用node创建一个待办事项的清单的web服务,设计到典型的创建,读取,更新和删除,操作。 用HTTP的谓词如GET,POST,PUT和DELETE,分别跟URL指定的资源的获取,创建&#xff0…

bilibiliC++64结构体-结构体定义和使用

8 结构体 8.1 结构体基本概念 结构体属于用户自定义的数据类型,允许用户存储不同的数据类型 8.2 结构体定义和使用 语法:struct 结构体名 { 结构体成员列表 }; 通过结构体创建变量的方式有三种: struct 结构体名 变量名stru…

ES6 let,const命令和块级作用域

最近准备开始读《react-native跨平台移动应用开发》,但是其封面的ES6成为主流,鼓励使用Promise机制,让我们拿起了阮一峰老师的《ES6标准入门》,准备系统学一下,之前有学过ES6,但没系统的看书,只是了解其中部…

bilibiliC++65结构体-结构体数组

8.3 结构体数组 作用: 将自定义的结构体放入到数组中方便维护 语法:struct 结构体名 数组名[元素个数] { {} , {} , ... {} } 示例: //结构体定义 struct student {//成员列表string name; //姓名int age; //年龄int score; //…

body-parser deprecated bodyParser: use individual json/urlencoded middlewares at server.js:4:10

最近在学习nodejs 基础中间件connect时,遇到了一些问题,因为一些旧有的api被废除,所以只要查一查新的手册就好啦。 Connect被定义为Node平台的中间件框架,其兼容性,稳定性和基础性都是很好的,而且express就…

bilibiliC++66结构体-结构体指针

8.4 结构体指针 作用: 通过指针访问结构体中的成员 利用操作符 ->可以通过结构体指针访问结构体属性 示例: //结构体定义 struct student {//成员列表string name; //姓名int age; //年龄int score; //分数 };int main() {struct studen…

Oracle导出程序Exp的使用具体过程

Oracle的导出实用程序(Export utility)允许从数据库提取数据,并且将数据写入操作系统文件。exp使用的基本格式:exp[username[/password[service]]],以下例举exp常用用法。 1. 获取帮助 exp helpy2. 导出一个完整数据库exp system/manager fil…