package cn.lmj.utils;





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 javax.sql.DataSource;





public class JdbcPool implements DataSource

{

//选用LinkedList方便移除链接

public static LinkedList<Connection> list = new LinkedList<Connection>();



static

{

try

{

Class.forName("com.mysql.jdbc.Driver");

//初始化20个链接

for(int i = 0;i<20;i++)

{

final Connection conn = DriverManager.getConnection("jdbc:mysql:///test","root","root");

//利用jdk动态代理实现增强Connection的close方法

Connection proxy = (Connection) Proxy.newProxyInstance(JdbcPool.class.getClassLoader(),conn.getClass().getInterfaces(),new InvocationHandler()

{

public Object invoke(Object obj, Method m, Object[] arg)

throws Throwable

{

if("close".equals(m.getName()))

{

//加到池中供其他线程訪问

list.addLast(conn);

}



return m.invoke(conn,arg);

}

});

list.add(proxy);

}

}

catch (Exception e)

{

e.printStackTrace();

}

}



public PrintWriter getLogWriter() throws SQLException

{

return null;

}





public int getLoginTimeout() throws SQLException

{

// TODO Auto-generated method stub

return 0;

}





public void setLogWriter(PrintWriter arg0) throws SQLException

{

// TODO Auto-generated method stub



}





public void setLoginTimeout(int arg0) throws SQLException

{

// TODO Auto-generated method stub



}





public boolean isWrapperFor(Class<?> arg0) throws SQLException

{

// TODO Auto-generated method stub

return false;

}





public <T> T unwrap(Class<T> arg0) throws SQLException

{

// TODO Auto-generated method stub

return null;

}





public Connection getConnection() throws SQLException

{

if(list.size()<=0)

{

new RuntimeException("数据库忙。稍后再来");

}

//移除第一个引用。不能用get,由于get不能移除引用

return list.removeFirst();

}





public Connection getConnection(String arg0, String arg1)

throws SQLException

{

// TODO Auto-generated method stub

return null;

}



}

java数据库连接池简单实现的更多相关文章

  1. Java数据库连接池

    转载过来的,最近在做一个小网站,准备使用这种方法.     Java jdbc数据库连接池总结! 1. 引言 近年来,随着Internet/Intranet建网技术的飞速发展和在世界范围内的迅速普及, ...

  2. Java数据库连接池的几种配置方法(以MySQL数据库为例)

    Java数据库连接池的几种配置方法(以MySQL数据库为例) 一.Tomcat配置数据源: 前提:需要将连接MySQL数据库驱动jar包放进Tomcat安装目录中common文件夹下的lib目录中 1 ...

  3. Java数据库连接池详解

    http://www.javaweb1024.com/java/JavaWebzhongji/2015/06/01/736.html 对于共享资源,有一个很著名的设计模式:资源池(Resource P ...

  4. 主流Java数据库连接池分析(C3P0,DBCP,TomcatPool,BoneCP,Druid)

    主流数据库连接池 常用的主流开源数据库连接池有C3P0.DBCP.Tomcat Jdbc Pool.BoneCP.Druid等 C3p0: 开源的JDBC连接池,实现了数据源和JNDI绑定,支持JDB ...

  5. [转帖]为什么HikariCP被号称为性能最好的Java数据库连接池,如何配置使用

    为什么HikariCP被号称为性能最好的Java数据库连接池,如何配置使用 原创Clement-Xu 发布于2015-07-17 15:53:14 阅读数 57066  收藏 展开 HiKariCP是 ...

  6. Java数据库连接池封装与用法

    Java数据库连接池封装与用法 修改于抄袭版本,那货写的有点BUG,两个类,一个用法 ConnectionPool类: package com.vl.sql; import java.sql.Conn ...

  7. 一个JAVA数据库连接池实现源码

    原文链接:http://www.open-open.com/lib/view/open1410875608164.html // // 一个效果非常不错的JAVA数据库连接池. // from:htt ...

  8. JAVA 数据库连接池(伪代码,简单易读)

    一.引言 近年来,随着 Internet/Intranet 建网技术的飞速发展和在世界范围内的迅速普及,电子商务的冲击波又一次在世界范围内掀起巨浪,各类商务网站吸引着大量用户的青睐,商务网站的访问量也 ...

  9. java数据库连接池技术简单使用

    JDBCDemo.java: package com.itheima.jdbc; import java.sql.Connection; import java.sql.PreparedStateme ...

随机推荐

  1. Install Oracle 11G Release 2 (11.2) on Centos Linux 7

    Install Oracle 11G Release 2 (11.2) on Centos Linux 7 This article presents how to install Oracle 11 ...

  2. AtCoder Regular Contest 083

    C - Sugar Water Time limit : 3sec / Memory limit : 256MB Score : 300 points Problem Statement Snuke ...

  3. 简单实用jstl实现代码编写

    package com.ceshi; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.s ...

  4. BZOJ4571 [Scoi2016]美味 【主席树】

    题目 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 bi,而他的偏好值为 xi .因此,第 i 位顾客认为第 ...

  5. LibreOJ2095 - 「CQOI2015」选数

    Portal Description 给出\(n,k,L,R(\leq10^9)\),求从\([L,R]\)中选出\(n\)个可相同有顺序的数使得其gcd为\(k\)的方案数. Solution 记\ ...

  6. 关闭chrome浏览器的input香蕉黄背景

    chrome浏览器input的自动完成,点击之后自动输入,input的背景会变成香蕉黄,用如下方法修复: /* Change the white to any color ;) 就是给input设置内 ...

  7. leetcode 20 简单括号匹配

    栈的运用 class Solution { public: bool isValid(string s) { stack<char>The_Stack; ; The_Stack.push( ...

  8. jquery插件的基本写法

    (function($){ var a={name:'2222',age:5555} var b={sex:'男',grade:5555} var c=$.extend({},a,b);//合并到新的 ...

  9. XMPP协议实现原理介绍(转)

    XMPP协议简介   XMPP(Extensible Messageing and Presence Protocol:可扩展消息与存在协议)是目前主流的四种IM(IM:instant messagi ...

  10. 转 Python爬虫入门一之综述

    转自: http://cuiqingcai.com/927.html 静觅 » Python爬虫入门一之综述 首先爬虫是什么? 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为 ...