java数据库连接池简单实现
来源:互联网 发布:php ceil 编辑:程序博客网 时间:2024/06/05 17:29
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
static
{
try
{
{
{
public Object invoke(Object obj, Method m, Object[] arg)
throws Throwable
{
if("close".equals(m.getName()))
}
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("数据库忙,稍后再来");
}
}
public Connection getConnection(String arg0, String arg1)
throws SQLException
{
// TODO Auto-generated method stub
return null;
}
}
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;
}
}
0 0
- java数据库连接池简单实现
- java实现简单的数据库连接池
- Java 的简单数据库连接池实现
- 用Java实现简单的数据库连接池
- java实现数据库连接池
- Java数据库连接池实现
- JAVA数据库连接池实现
- Java 数据库连接池 实现
- JAVA数据库连接池实现
- JAVA数据库连接池实现
- JAVA数据库连接池实现
- JAVA数据库连接池实现
- JAVA数据库连接池实现
- JAVA数据库连接池实现
- JAVA数据库连接池实现
- 数据库连接池的简单实现
- 数据库连接池的简单实现
- 数据库连接池的简单实现
- Eddy's AC难题 2200
- 链表算法二之单链表逆置
- CentOS 设置命令别名 并且在全系统有效
- Android 完美实现图片圆角和圆形(对实现进行分析)
- VMware Workstation 9.0虚拟机装XP系统,开机声音沙哑?
- java数据库连接池简单实现
- 求二叉树中的叶子数
- 软件开发模式对比(瀑布、迭代、螺旋、敏捷)
- 背包问题(一)
- 深入浅出讲解单例模式(java实现)
- Hangover
- GridView组件使用
- 什么是句柄什么是句柄对象
- 我技术能力很NB吗?