• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mysql 搞代码 4年前 (2022-01-09) 21次浏览 已收录 0个评论

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){			f<div>本文来源gaodai.ma#com搞#代!码网_</div>inal 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();				}			}		}	}}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:数据源的编写(开发中不写)、使用动态代理覆写Connection的clos
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址