使用代理创建连接池 proxyPool
配置文件properties
url=jdbc:mysql://127.0.0.1:3306/mine?characterEncoding=UTF-8
user=root
password=1234
driverClass=com.mysql.jdbc.Driver
主要代码
package JDBCUtils; import java.io.IOException;
import java.io.InputStream;
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.util.LinkedList;
import java.util.Properties;
/**
* 使用代理创建连接池
* @author ASUS
*
*/
public class ProxyConnUtils { private static LinkedList<Connection> pool = new LinkedList<>();
private static String url;
private static String user;
private static String password;
private static String driverClass;
//private static Connection conn;
static{
try {
Properties properties = new Properties();
InputStream in = ProxyConnUtils.class.getResourceAsStream("/db.properties");
properties.load(in);
url = properties.getProperty("url");
System.err.println(url);
user = properties.getProperty("user");
password = properties.getProperty("password");
driverClass = properties.getProperty("driverClass");
Class.forName(driverClass);
for(int i = 0;i<3;i++){
final Connection conn = DriverManager.getConnection(url, user, password);
//对connection做代理
Object connProxy = Proxy.newProxyInstance(ProxyConnUtils.class.getClassLoader(),
new Class[]{Connection.class},
new InvocationHandler() { @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//判断是否是close方法 回收连接
if(method.getName().equals("close")){
synchronized (pool) {
System.err.println("不能关闭");
pool.addLast((Connection) proxy);
pool.notify();
return null;
}
}else{
//若果调用的不是close方法 直接放行
return method.invoke(conn, args);
}
}
});
// 将代理对象添加到池中去
pool.add((Connection) connProxy);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} //获取connection连接
public static Connection getConnection(){
synchronized (pool) {
if(pool.size() == 0){
try {
pool.wait();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
Connection connection = pool.removeFirst();
return connection;
}
}
}
测试代码
package JDBCTest;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Scanner;
import JDBCUtils.ConnUtil;
import JDBCUtils.ConnUtils;
import JDBCUtils.ProxyConnUtils;
public class Demo01_tx3 {
class one extends Thread{
@Override
public void run() {
System.err.println("1:获取连接");
Connection con =
ProxyConnUtils.getConnection();
try{
System.err.println("2:打开事务");
con.setAutoCommit(false);
System.err.println("3:写入Jack");
Statement st = con.createStatement();
st.execute("insert into money(id,name) values(1,'Jack')");
System.err.println("4:提交 ..");
con.commit();
}catch(Exception e){
e.printStackTrace();
}finally {
try {
System.err.println("5:关闭连接");
con.setAutoCommit(true);
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
}; public Demo01_tx3() {
for(int i=0;i<5;i++){
new one().start();
}
} public static void main(String[] args) {
new Demo01_tx3();
}
}
运行结果

使用代理创建连接池 proxyPool的更多相关文章
- Junit 注解 类加载器 .动态代理 jdbc 连接池 DButils 事务 Arraylist Linklist hashset 异常 哈希表的数据结构,存储过程 Map Object String Stringbufere File类 文件过滤器_原理分析 flush方法和close方法 序列号冲突问题
Junit 注解 3).其它注意事项: 1).@Test运行的方法,不能有形参: 2).@Test运行的方法,不能有返回值: 3).@Test运行的方法,不能是静态方法: 4).在一个类中,可以同时定 ...
- nodejs mysql 创建连接池
用Nodejs连接MySQL 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javas ...
- Java创建连接池连接不同数据库
在一个应用里面,可能涉及到连接多个不同数据库进行操作,而每次连接写不同的实现会很麻烦.前面已经会了用JDBC连接数据库,那么利用反射和工厂模式,可以实现连接不同的数据库,这样处理起来将会很方便.同时建 ...
- jdk 动态代理 数据连接池
package com.itheima.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandl ...
- Python 使用 PyMysql、DBUtils 创建连接池,提升性能
转自:https://blog.csdn.net/weixin_41287692/article/details/83413775 Python 编程中可以使用 PyMysql 进行数据库的连接及诸如 ...
- MySQL_(Java)【连接池】简单在JDBCUtils.java中创建连接池
MySQL_(Java)[事物操作]使用JDBC模拟银行转账向数据库发起修改请求 传送门 MySQL_(Java)[连接池]使用DBCP简单模拟银行转账事物 传送门 Java应用程序访问数据库的过程: ...
- 使用第三方组件(django-redis)创建连接池
settings里面: ##redis配置CACHES={ 'default':{ 'BACKEND':'django_redis.cache.RedisCache', 'LOCATION':'red ...
- druid:java代码创建连接池
PropertiesDB 是一个读取配置文件的类,也可以不用,每个参数直接用String代替. public DataSource dataSource(PropertiesDB properties ...
- JDK动态代理连接池
JDK动态代理 1 什么是JDK动态代理 刚刚写ItcastConnection时爽么?因为Connection中的方法太多了,每个都要写,所以很累吧.累点到是没什么,可以完成功能就是好的.但是不 ...
随机推荐
- 控件_TimePicker
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view ...
- C#常见委托のdelegate定义,Func,Action,Predicate总结
委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板:(交流可以加qq群:435226676) 委托常见的方式有一般委托显示定义,Func<T,TResult> (T, ...
- Android——Intent和Intent过滤器
http://www.cnblogs.com/XP-Lee/p/3613830.html Intent就是一个激活组件的消息对象,用于组件之间的通信.需要注意的是,能被Intent激活通信的组件只有三 ...
- 【Java多线程】线程状态、线程池状态
线程状态: 线程共包括以下5种状态.1. 新建状态(New) 线程对象被创建后,就进入了新建状态.例如,Thread thread = new Thread().2. 就绪状态(Runnable) 也 ...
- day14 Python集合
定义:由不同元素组成的集合,集合是一组无序排列的可hash值,可以作为字典的key 1.不同元素.2.无序.3.集合中元素必须是不可变类型(数字,字符串,元祖) 特性:集合的目的是将不同的值存放在一起 ...
- Linux下rz/sz安装及使用方法
新搞的云服务器用SecureCRT不支持上传和下载,没有找到rz命令.记录一下如何安装rz/sz命令的方法. 一.工具说明 在SecureCRT这样的ssh登录软件里, 通过在Linux界面里输入rz ...
- PAT A1144 The Missing Number (20 分)——set
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...
- Asp.net中web.config配置文件详解(一)
本文摘自Asp.net中web.config配置文件详解 web.config是一个XML文件,用来储存Asp.NET Web应用程序的配置信息,包括数据库连接字符.身份安全验证等,可以出现在Asp. ...
- 对int array进行排序
今天再学习一些C#的基础知识,如对 Int Array进行排序: 你可以在控制台应用程序中,创建一个类别,它属性和2个构造函数: class Af { private int[] myVar; pub ...
- python 常见矩阵运算
python 的 numpy 库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入 numpy 的包. 1.numpy 的导入和使用 from numpy import *;#导入numpy的 ...