标签:
配置文件properties
1 url=jdbc:mysql://127.0.0.1:3306/mine?characterEncoding=UTF-8
2 user=root
3 password=1234
4 driverClass=com.mysql.jdbc.Driver
主要代码
1 package JDBCUtils; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.lang.reflect.InvocationHandler; 6 import java.lang.reflect.Method; 7 import java.lang.reflect.Proxy; 8 import java.sql.Connection; 9 import java.sql.DriverManager; 10 import java.util.LinkedList; 11 import java.util.Properties; 12 /** 13 * 使用代理创建连接池 14 * @author ASUS 15 * 16 */ 17 public class ProxyConnUtils { 18 19 private static LinkedList<Connection> pool = new LinkedList<>(); 20 private static String url; 21 private static String user; 22 private static String password; 23 private static String driverClass; 24 //private static Connection conn; 25 static{ 26 try { 27 Properties properties = new Properties(); 28 InputStream in = ProxyConnUtils.class.getResourceAsStream("/db.properties"); 29 properties.load(in); 30 url = properties.getProperty("url"); 31 System.err.println(url); 32 user = properties.getProperty("user"); 33 password = properties.getProperty("password"); 34 driverClass = properties.getProperty("driverClass"); 35 Class.forName(driverClass); 36 for(int i = 0;i<3;i++){ 37 final Connection conn = DriverManager.getConnection(url, user, password); 38 //对connection做代理 39 Object connProxy = Proxy.newProxyInstance(ProxyConnUtils.class.getClassLoader(), 40 new Class[]{Connection.class}, 41 new InvocationHandler() { 42 43 @Override 44 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 45 //判断是否是close方法 回收连接 46 if(method.getName().equals("close")){ 47 synchronized (pool) { 48 System.err.println("不能关闭"); 49 pool.addLast((Connection) proxy); 50 pool.notify(); 51 return null; 52 } 53 }else{ 54 //若果调用的不是close方法 直接放行 55 return method.invoke(conn, args); 56 } 57 } 58 }); 59 // 将代理对象添加到池中去 60 pool.add((Connection) connProxy); 61 } 62 } catch (Exception e) { 63 e.printStackTrace(); 64 throw new RuntimeException(e); 65 } 66 } 67 68 //获取connection连接 69 public static Connection getConnection(){ 70 synchronized (pool) { 71 if(pool.size() == 0){ 72 try { 73 pool.wait(); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 throw new RuntimeException(e); 77 } 78 } 79 Connection connection = pool.removeFirst(); 80 return connection; 81 } 82 } 83 }
测试代码
1 package JDBCTest; 2 import java.sql.Connection; 3 import java.sql.Statement; 4 import java.util.Scanner; 5 import JDBCUtils.ConnUtil; 6 import JDBCUtils.ConnUtils; 7 import JDBCUtils.ProxyConnUtils; 8 public class Demo01_tx3 { 9 class one extends Thread{ 10 @Override 11 public void run() { 12 System.err.println("1:获取连接"); 13 Connection con = 14 ProxyConnUtils.getConnection(); 15 try{ 16 System.err.println("2:打开事务"); 17 con.setAutoCommit(false); 18 System.err.println("3:写入Jack"); 19 Statement st = con.createStatement(); 20 st.execute("insert into money(id,name) values(1,‘Jack‘)"); 21 System.err.println("4:提交 .."); 22 con.commit(); 23 }catch(Exception e){ 24 e.printStackTrace(); 25 }finally { 26 try { 27 System.err.println("5:关闭连接"); 28 con.setAutoCommit(true); 29 con.close(); 30 } catch (Exception e) { 31 e.printStackTrace(); 32 } 33 } 34 }; 35 }; 36 37 public Demo01_tx3() { 38 for(int i=0;i<5;i++){ 39 new one().start(); 40 } 41 } 42 43 public static void main(String[] args) { 44 new Demo01_tx3(); 45 } 46 }
运行结果
标签:
原文地址:http://www.cnblogs.com/fujilong/p/5568299.html