数据源的编写(开发中不写)、使用动态代理覆写Connection的close方法
来源:互联网 发布:mac系统玩不了魔兽 编辑:程序博客网 时间:2024/05/23 07:23
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.LinkedList;import javax.sql.DataSource;import com.itheima.util.JdbcUtil;//标准的数据库连接池。按照字面意思,一般叫做数据源(都是带池的,为了提高效率)public class MyDataSource implements DataSource {private static LinkedList<Connection> pool = new LinkedList<Connection>();static{try {//池进行初始化:10个连接for(int i=0;i<10;i++){pool.add(JdbcUtil.getConnection());}} catch (Exception e) {throw new ExceptionInInitializerError("数据库连接池初始化失败");}}//返回它的包装类,会拦截connection中的所有方法@Overridepublic synchronized Connection getConnection() throws SQLException {if(pool.size()>0){final Connection conn = pool.removeFirst();//原来的数据的connection实现。被包装类Connection proxyconn = (Connection)Proxy.newProxyInstance(conn.getClass().getClassLoader(),conn.getClass().getInterfaces(), new InvocationHandler() {public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {System.out.println(method.getName());if("close".equals(method.getName())){return pool.add(conn);}else{return method.invoke(conn, args);}}});return proxyconn;}elsethrow new RuntimeException("服务器忙");}@Overridepublic PrintWriter getLogWriter() throws SQLException {return null;}@Overridepublic void setLogWriter(PrintWriter out) throws SQLException {}@Overridepublic void setLoginTimeout(int seconds) throws SQLException {}@Overridepublic int getLoginTimeout() throws SQLException {return 0;}@Overridepublic <T> T unwrap(Class<T> iface) throws SQLException {return null;}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {return false;}@Overridepublic Connection getConnection(String username, String password)throws SQLException {return null;}}
package com.jxnu.util;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;//通用的工具类:与具体数据库没有关系public class JdbcUtil {private static String driverClass;private static String url;private static String username;private static String password;static{try {//加载配置文件InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dbcfg.properties");Properties props = new Properties();props.load(in);driverClass = props.getProperty("driverClass");url = props.getProperty("url");username = props.getProperty("username");password = props.getProperty("password");Class.forName(driverClass);} catch (Exception e) {throw new ExceptionInInitializerError(e);}}//获取数据库的连接public static Connection getConnection() throws Exception{Connection conn = DriverManager.getConnection(url, username, password);return conn;}//释放占用的资源public static void release(ResultSet rs,Statement stmt,Connection conn){if(rs!=null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if(stmt!=null){try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}
测试:
package com.jxnu.pool04;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import org.junit.Test;public class Dao1Impl {MyDataSource ds = new MyDataSource();@Testpublic void add(){Connection conn = null;Statement stmt = null;try{conn = ds.getConnection();stmt = conn.createStatement();//com.mysql.jdbc.Connection :MySQL//com.oracle.jdbc.Connection :Oracle//...System.out.println(conn.getClass().getName());}catch(Exception e){e.printStackTrace();}finally{if(conn!=null){try {conn.close();//把连接给关了。不应该关,放回池中,怎么放回池中呢?连接池重点所在} catch (SQLException e) {e.printStackTrace();}}}}}
0 0
- 数据源的编写(开发中不写)、使用动态代理覆写Connection的close方法
- 动态代理实现数据库连接池功能(代理Connection的close方法)
- 运用动态代理来检测未close的JDBC Connection
- C3p0数据源ComboPooledDataSource的close方法
- 使用JDK动态代理方式实现自己的数据源
- 数据库 Connection的dispose和close方法差异
- Connection.close()的实现解析
- swift__UITableView 的数据源方法 和 代理方法
- 开发动态的Inventory数据源
- 动态设置数据源的方法
- java.sql.Connection的close方法究竟干了啥(以MySQL为例)
- 使用动态代理完成字符集编码过滤器的编写
- iOS UITableView数据源与代理方法的调用(一)
- tableView的数据源及代理方法
- DBCP数据源配置,数据源时间不使用出现Could not close JDBC Connection
- DBCP数据源配置,数据源时间不使用出现Could not close JDBC Connection
- 动态代理的实现2-编写可生成代理和插入通告的通用方法
- 动态代理的实现2-编写可生成代理和插入通告的通用方法 .
- 深入理解java异常处理机制
- 第二周作业
- yii分页组件的使用
- 用MonoDevelop调试Unity3D脚本
- 一些内核学习网站和博客
- 数据源的编写(开发中不写)、使用动态代理覆写Connection的close方法
- 深入Java Function: Class.forName()
- E观天下:中国应该如何拥抱大数据时代?
- 内核中的多点触摸协议文档 Multi-touch (MT) Protocol
- Android学习笔记(四)模拟器加速
- intellij idea 添加JDK 并创建项目
- jquery插件开发
- 在给定的数组中找出两个元素和为给定值的所有元素对
- Java类型转化报错