package com.itheima.datasource;

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.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.sql.DataSource; import com.itheima.utils.JdbcUtil; /**
* 引入Sun标准数据源接口 为了更多的人去用
* DataSource :javax.sql.DataSource
*
* @author wangli
*
*/
public class MyDataSource3 implements DataSource {
public static List<Connection> pool = new ArrayList<Connection>();//池子
private static int size=10;
public static void setSize(int size) {
MyDataSource3.size = size;
} //初始化池中的连接
static{
try {
for (int i = 0; i < size; i++) {
pool.add(JdbcUtil.getConnection());
}
} catch (Exception e) {
e.printStackTrace();
} } //统一提供方法,用于从池中获取连接
public synchronized Connection getConnection(){
if(pool.size()>0){
final Connection con = pool.remove(0);//删除这个连接,因为别人不能此时不能再用它 final 用匿名内部类里 con代表数据连接池里的池连接接口 //代理模式 JDK自带的相应代理相关类
//Proxy动态代理相关类
//Proxy
// newProxyInstance(ClassLoader cl,Class<?>[] interfaces,InvocationHandler ih)
// ClassLoader :代表类加载器 被代理的对象用什么类加载器,代理对象就要用什么加载器
//interfaces :代表所实现的接口,被代理对象实现什么接口,代理对象就要实现什么接口
//InvocationHandler 处理器 就是要处理相关的緢节问题 一般有一个匿名内部类
//InvocationHandler 策略模式
Connection proxyConn = (Connection) Proxy.newProxyInstance(con.getClass().getClassLoader(), con.getClass().getInterfaces(),
new InvocationHandler() { @Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("close".equals(method.getName())){
//对于close方法要改写 添加到池中
return pool.add(con);//加入池中
}else{
//对于其它方法,用mysql的connection对象的底层实现
return method.invoke(con, args); 被代理对象和,被代理对象的方法里的参数
}
}
}
);
return proxyConn;
}else{
throw new RuntimeException("池中无连接");
} } //还回一个连接到池中
public static void close(Connection con){
pool.add(con);//将这个连接回收到池中
} @Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
} @Override
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub } @Override
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub } @Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
} @Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
} @Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
} @Override
public Connection getConnection(String username, String password)
throws SQLException {
// TODO Auto-generated method stub
return null;
} }

最新文章

  1. C#-WebForm-★★★ 分页展示 ★★★
  2. iOS开发_内存泄漏、内存溢出和野指针之间的区别
  3. Yii2的深入学习--自动加载机制
  4. 开发板上使用core文件调试
  5. SVG裁剪和平移的顺序
  6. &quot;Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list.xml,reason: Connection
  7. ASP.NET Email + WebConfig
  8. C语言中typedef
  9. C#判断操作系统是32位还是64位(超简单)
  10. Spring的5种事物配置方式
  11. 关于启动文件分析的(MDK-ARM) 【转】
  12. ELK-filebeat收集日志到Kafka,并转存ES
  13. vue踩坑--TypeError: __WEBPACK_IMPORTED_MODULE_1_vuex__.a.store is not a constructor
  14. Docker系列01—容器的发展历程---Docker的生态圈
  15. AJAX初识(原生JS版AJAX和Jquery版AJAX)
  16. mysql执行语句提示Table &#39;performance_schema.session_variables&#39; doesn&#39;t exist
  17. 性能监控(3)–linux下的iostat命令
  18. mysql排序索引优化
  19. 以太坊erc20转账失败的情况和原因
  20. AutoMatic merge failed;fix conflicts and then commit the result.解决方法

热门文章

  1. oracle connect by 递归,反递归,自动补全查询实现
  2. Net.Core导入EXCel文件里的数据
  3. iOS开发--OC常见报错
  4. [SinGuLaRiTy] NOIP模拟赛(TSY)-Day 1
  5. [SinGuLaRiTy] 图论题目复习
  6. Java异常处理方式
  7. Java基础之对包,类,方法,变量理解(灵感)
  8. kuangbin专题16D(next求最小循环节)
  9. centOS系统将php升级到5.6 安装扩展
  10. Redis及虚拟机windows两种环境安装配置