使用代理创建连接池 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中的方法太多了,每个都要写,所以很累吧.累点到是没什么,可以完成功能就是好的.但是不 ...
随机推荐
- 【19】Linux系统知识点
一.积跬步以致千里,积怠情以致深渊 二.目录结构
- 17秋 软件工程 团队第五次作业 Alpha Scrum10
17秋 软件工程 团队第五次作业 Alpha Scrum10 今日完成的任务 世强:Android客户端成员列表完善.APP前端子部门和活动中心界面与数据交互: 港晨:Web前端主页的接口对接: 树民 ...
- MySQL表名区分大小写设置
关闭MySQL服务 在服务运行目录找到my.ini或者my.cnf文件 打开文件,找到[mysqld]在下面增加一行 lower_case_table_names=0 (0:大小写敏感;1:大小写不敏 ...
- WPF之托盘图标的设定
首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Collections.Generic; using ...
- WPF防止重复运行实例
1.方法一 在app.xaml.cs下添加如下代码: /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partia ...
- swift的@objc总结
One can explicitly write @objc on any Swift declaration that can be expressed in Objective-C. @objc相 ...
- Redis String类型的API使用
package com.daxin.jedis_datastructure; import org.junit.After; import org.junit.Before; import org.j ...
- kafka监控kafka-eagle 容器化配置
由于kafka.zk 集群已经部署在k8s中, kafka的服务名 kafka-hs, zk的服务名为:zk-cs ,对kafka进行监控,所以需要把监控部署到k8s中,选择使用kafka-eagl ...
- Ros使用Arduino 2 使用rosserial创建一个publisher
1 启动arduino 将arduino开发板连接到电脑的usb口,在arduino IDE中进行设置. 选择Tools->Board,选择你所使用的arduino开发板的类型,所使用的ardu ...
- zabbix学习-zabbix安装
本次安装教程完全参考官方rpm安装教程: https://www.zabbix.com/documentation/3.4/zh/manual/installation/install_from_pa ...