您好,欢迎访问一九零五行业门户网

数据源的编写(开发中不写)、使用动态代理覆写Connection的clos

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.datas
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 pool = new linkedlist(); static{ try { //池进行初始化:10个连接 for(int i=0;i0){ 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; }else throw new runtimeexception(服务器忙); } @override public printwriter getlogwriter() throws sqlexception { return null; } @override public void setlogwriter(printwriter out) throws sqlexception { } @override public void setlogintimeout(int seconds) throws sqlexception { } @override public int getlogintimeout() throws sqlexception { return 0; } @override public t unwrap(class iface) throws sqlexception { return null; } @override public boolean iswrapperfor(class iface) throws sqlexception { return false; } @override public 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(); @test public 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(); } } } }}
其它类似信息

推荐信息