配置文件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的更多相关文章

  1. Junit 注解 类加载器 .动态代理 jdbc 连接池 DButils 事务 Arraylist Linklist hashset 异常 哈希表的数据结构,存储过程 Map Object String Stringbufere File类 文件过滤器_原理分析 flush方法和close方法 序列号冲突问题

    Junit 注解 3).其它注意事项: 1).@Test运行的方法,不能有形参: 2).@Test运行的方法,不能有返回值: 3).@Test运行的方法,不能是静态方法: 4).在一个类中,可以同时定 ...

  2. nodejs mysql 创建连接池

    用Nodejs连接MySQL 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javas ...

  3. Java创建连接池连接不同数据库

    在一个应用里面,可能涉及到连接多个不同数据库进行操作,而每次连接写不同的实现会很麻烦.前面已经会了用JDBC连接数据库,那么利用反射和工厂模式,可以实现连接不同的数据库,这样处理起来将会很方便.同时建 ...

  4. jdk 动态代理 数据连接池

    package com.itheima.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandl ...

  5. Python 使用 PyMysql、DBUtils 创建连接池,提升性能

    转自:https://blog.csdn.net/weixin_41287692/article/details/83413775 Python 编程中可以使用 PyMysql 进行数据库的连接及诸如 ...

  6. MySQL_(Java)【连接池】简单在JDBCUtils.java中创建连接池

    MySQL_(Java)[事物操作]使用JDBC模拟银行转账向数据库发起修改请求 传送门 MySQL_(Java)[连接池]使用DBCP简单模拟银行转账事物 传送门 Java应用程序访问数据库的过程: ...

  7. 使用第三方组件(django-redis)创建连接池

    settings里面: ##redis配置CACHES={ 'default':{ 'BACKEND':'django_redis.cache.RedisCache', 'LOCATION':'red ...

  8. druid:java代码创建连接池

    PropertiesDB 是一个读取配置文件的类,也可以不用,每个参数直接用String代替. public DataSource dataSource(PropertiesDB properties ...

  9. JDK动态代理连接池

    JDK动态代理   1 什么是JDK动态代理 刚刚写ItcastConnection时爽么?因为Connection中的方法太多了,每个都要写,所以很累吧.累点到是没什么,可以完成功能就是好的.但是不 ...

随机推荐

  1. Leviticus

    The head is empty and empty. Just practicing English will not have any effect. The best effect is to ...

  2. 初级算法-6.两个数组的交集 II

    题目描述: 给定两个数组,编写一个函数来计算它们的交集. 示例 : 输入: nums1 = [,,,], nums2 = [,] 输出: [,] 示例 : 输入: nums1 = [,,], nums ...

  3. SQL 公用表达式CTE

    一 基本用法 with mywith as(select * from Student ) select * from mywith 二 递归调用 with mywith as( select ID, ...

  4. Android学习之基础知识五—RecyclerView(滚动控件)

    RecyclerView可以说是增强版的ListView,不仅具有ListVIew的效果,还弥补许多ListView的不足. 一.RecyclerView的基本用法 与百分比布局类似,Recycler ...

  5. UI自动化测试模型

    所谓的自动化测试模型,可以理解为自动化测试框架+工具设计的一种思想产物. 先说说库.框架.工具之间的区别: 库:英文名Library,由代码集成的一个产品,供用户调用.面向对象的库叫做类库,面向过程的 ...

  6. 窥看 SpringBoot 的原理与使用

    一:SpringBoot的启动 1. 继承spring-boot-starter-parent项目 2. 导入spring-boot-dependencies项目依赖 二:Spring Boot 主类 ...

  7. JS-详解算数运算符"+"

    二元加法运算符“+”可以对两个数字做加法,也可以做字符串连接操作: 当两个操作数都是数字或都是字符串的时候,计算结果是显而易见的.然而对于其他情况来说,则要进行一些必要的类型转换,并且运算符的行为依赖 ...

  8. 练习ng-show和ng-hide的方法

    在程序设计过程,我们需要把某一元素或是或一块进行显示与隐藏. 如你正使用angularjs的话,就可以使用ng-show或者ng-hide来进行控制. var showhideApp = angula ...

  9. (原创)odoo在docker环境下无法备份

    odoo容器内置postgresql-client版本和数据库版本不一致,安装和数据库版本相同或者更高版本的客户端 参考:https://www.postgresql.org/download/lin ...

  10. Luogu P4462 [CQOI2018]异或序列

    一道稍微要点脑子的莫队题,原来省选也会搬CF原题 首先利用\(xor\)的性质,我们可以搞一个异或前缀和的东西 每一次插入一个数,考虑它和之前已经加入的数能产生多少贡献 记一下之前的异或总值,然后还是 ...