`
u011936142
  • 浏览: 43055 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

同过增强Connection类[重写了close的方法]实现的从连接池取出连接并放回连接的简单的实现流程

 
阅读更多
package tk.dong.connection.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.logging.Logger;

import javax.sql.DataSource;

//这是同过增强Connection类[重写了close的方法]实现的从连接池取出连接并放回连接
public class JdbcPool implements DataSource {

	// 创建连接池,用的是LinkList,
	private static LinkedList<Connection> connections = new LinkedList<Connection>();

	// 通过静态初始化块创建,当程序进行初始化的时候就会触发

	static {
		// 将连接数据库的配置文件读入到流中
		InputStream inStream = JdbcPool.class.getClassLoader().getResourceAsStream(
				"jdbc.properties");
		Properties properties = new Properties();
		try {
			properties.load(inStream);

			// 获取连接数据库的驱动(根据property属性文件中的属性获取驱动)
			Class.forName(properties.getProperty("driverClassName"));
			for (int i = 0; i < 10; i++) {
				// 获取conn连接连接对象
				Connection conn = DriverManager.getConnection(
						properties.getProperty("url"),
						properties.getProperty("user"),
						properties.getProperty("pass"));

				// 这是在装入连接池的时候进行的操作处理,将connection中的close进行改写
				MyConnection myConnection = new MyConnection(conn, connections);

				// 将处理后的连接对象存入连接池
				connections.add(myConnection);
				System.out.println("连接池已经加入了::::::::::" + connections.size()
						+ "::::::::::个链接对象");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 这两个是从连接池中获取连接的方法
	@Override
	public Connection getConnection() throws SQLException {

		//生命连接对象
		Connection conn=null;
		if(connections.size()>0){
			//取出链表中的第一个对象赋值给临时的连接对象
			conn=connections.removeFirst();
			System.out.println("又一个连接对象被拿走:::::::连接池还有"+connections.size()+"个连接对象");
		}
		return conn;
	}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

}

// 增强的Connection 连接对类
// 装饰器模式
// 1.首先看需要被增强对象继承了什么接口或父类,编写一个类也去继承这些接口或父类。
class MyConnection implements Connection {
	// 2.在类中定义一个变量,变量类型即需增强对象的类型。
	// 用来接收连接池
	private LinkedList<Connection> connections;
	// 用来接收连接对象
	private Connection conn;

	// 构造器为
	public MyConnection(Connection conn, LinkedList<Connection> connections) {
		this.conn = conn;
		this.connections = connections;
	}

	// 我只用到这个方法所以我只写这个方法
	@Override
	public void close() throws SQLException {
		// 将不用的连接再次放入连接池
		connections.add(conn);
		System.out.println("有一个连接对象用完了,已经返回连接池,现在连接池中还有==="+connections.size());
	}

	// 下面的方法用不到,所以就不写内容了
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Statement createStatement() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PreparedStatement prepareStatement(String sql) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CallableStatement prepareCall(String sql) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String nativeSQL(String sql) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setAutoCommit(boolean autoCommit) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean getAutoCommit() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void commit() throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void rollback() throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isClosed() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public DatabaseMetaData getMetaData() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setReadOnly(boolean readOnly) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isReadOnly() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setCatalog(String catalog) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public String getCatalog() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setTransactionIsolation(int level) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public int getTransactionIsolation() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public SQLWarning getWarnings() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void clearWarnings() throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public Statement createStatement(int resultSetType, int resultSetConcurrency)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Class<?>> getTypeMap() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setHoldability(int holdability) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public int getHoldability() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Savepoint setSavepoint() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Savepoint setSavepoint(String name) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void rollback(Savepoint savepoint) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public Statement createStatement(int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PreparedStatement prepareStatement(String sql, String[] columnNames)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Clob createClob() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Blob createBlob() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public NClob createNClob() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SQLXML createSQLXML() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isValid(int timeout) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setClientInfo(String name, String value)
			throws SQLClientInfoException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setClientInfo(Properties properties)
			throws SQLClientInfoException {
		// TODO Auto-generated method stub

	}

	@Override
	public String getClientInfo(String name) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Properties getClientInfo() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Array createArrayOf(String typeName, Object[] elements)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Struct createStruct(String typeName, Object[] attributes)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setSchema(String schema) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public String getSchema() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void abort(Executor executor) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setNetworkTimeout(Executor executor, int milliseconds)
			throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public int getNetworkTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

}
测试代码
package tk.dong.connectionPool.test;


import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

import tk.dong.connection.util.JdbcPool;

public class TestJdbcPool {

	@Test
	public void jdbcPool() throws SQLException {
		// 创建连接池对象
		JdbcPool jdbcPool = new JdbcPool();
		// 从连接池中获取连接对象
		jdbcPool.getConnection();
		// 多次获取连接对象
		jdbcPool.getConnection();
		jdbcPool.getConnection();
		Connection conn = jdbcPool.getConnection();
		// 归还用完的连接对象
		conn.close();
		// 再次获取连接对象
		jdbcPool.getConnection();
		jdbcPool.getConnection();
		jdbcPool.getConnection();
		jdbcPool.getConnection();

	}

}
测试输出结果
连接池已经加入了::::::::::1::::::::::个链接对象
连接池已经加入了::::::::::2::::::::::个链接对象
连接池已经加入了::::::::::3::::::::::个链接对象
连接池已经加入了::::::::::4::::::::::个链接对象
连接池已经加入了::::::::::5::::::::::个链接对象
连接池已经加入了::::::::::6::::::::::个链接对象
连接池已经加入了::::::::::7::::::::::个链接对象
连接池已经加入了::::::::::8::::::::::个链接对象
连接池已经加入了::::::::::9::::::::::个链接对象
连接池已经加入了::::::::::10::::::::::个链接对象
又一个连接对象被拿走:::::::连接池还有9个连接对象
又一个连接对象被拿走:::::::连接池还有8个连接对象
又一个连接对象被拿走:::::::连接池还有7个连接对象
又一个连接对象被拿走:::::::连接池还有6个连接对象
有一个连接对象用完了,已经返回连接池,现在连接池中还有===7
又一个连接对象被拿走:::::::连接池还有6个连接对象
又一个连接对象被拿走:::::::连接池还有5个连接对象
又一个连接对象被拿走:::::::连接池还有4个连接对象
又一个连接对象被拿走:::::::连接池还有3个连接对象

分享到:
评论

相关推荐

    okhttp中连接池实现

    代码中包含okhhtp中连接池的设计,包含连接对象的添加,连接对象何时被移除。

    ADO.NET连接池示例

    当我们调用Close或者Dispose方法时,实际并不断开连接,而是把连接放回连接池,再次使用时候重连接池中取得空闲资源。因为打开和关闭数据库连接开销比较大,所以连接池对于与数据库链接资源的控制上,加快客户端程序...

    自己实现的ActiveMQ连接池和新版本ActiveMQ自带的连接池,封装好的工具类,可直接使用

    自己实现的ActiveMQ连接池和新版本ActiveMQ自带的连接池,封装好的工具类,可直接使用

    使用Java编写的RabbitMQ连接池方法

    RabbitMQ客户连接池的Java实现。我们刚开始也是采用这种方式来实现的,但做压力测试时,发现这种每次新建Connection和新建Channel是非常耗时的,在大并发下,一般都要8毫秒左右,慢的话,好多都是几十毫秒。因此我们...

    thinkphp5-swoole 数据库连接池实现

    基于tp5的swoole支持,对th5的connection进行改造,使用Swoole\Coroutine\MySQL重写了基于swoole的PDO接口,实现了mysql的数据库连接池,本地测试可用。使用时,替换thinkphp/library/think/db/Connection.php,并...

    RabbitMQ连接池+SpringBoot实现

    RabbitMQ连接池+SpringBoot实现。通过连接池实现将高效的管理RabbitMQ的Connection,并与springboot进行整合,实现消息发送,获取队列列表等功能。基于此可以进行更多功能的扩充。

    连接池原来这么简单

    上述伪代码忽略了一些细节,在实现连接池...(4)通过freeArray,connectionMap可以让取连接和放回连接都达到O(1)时间复杂度 (5)可以通过hash实现id串行化 (6)负载均衡、故障转移、服务自动扩容都可以在这一层实现

    ConnectionPool 基于java实现数据库连接池

    java实现数据库连接池的模拟

    基于Tomcat的数据库连接池配置和测试 的三种方法

    数据连接池的工作机制:J2EE服务器启动时会建立一定数量的池连接,并...实现方式,返回的Connection是原始Connection的代理,代理Connection的close方法不是真正关连接,而是把它代理的Connection对象还回到连接池中。

    Qt 多线程连接数据库——数据库连接池

    * 获取连接时不需要了解连接的名字,连接池内部维护连接的名字 * 支持多线程,保证获取到的连接一定是没有被其他线程正在使用 * 按需创建连接,可以创建多个连接,可以控制连接的数量 * 连接被复用,不是每次都...

    R2数据库连接池高性能连接池v1.3

    conn用完后必须关闭,不然池中连接会被用完(原理:关闭conn时放回池中)。 多池调用方式为R2Pool pool1=R2PoolUtil.getPool(new File("第一个连接池的配置文件.properties")); Connnection conn1 = pool1.get...

    Java对JDBC连接池的实现

    这是我写的一个对JDBC连接池的实现,高手见了可不要笑啊!!!! 程序是在linux下用Eclipse下编写的. 用Jude进行建模,数据库使用了mysql. 程序自带了MySql的Connection连接驱动类,你也可以使用别的驱动类和数据库, 在src/...

    java数据库连接池connectionPool.zip

    java数据库连接池connectionPool.zip java数据库连接池connectionPool.zip java数据库连接池connectionPool.zip java数据库连接池connectionPool.zip java数据库连接池connectionPool.zip

    proxool连接池配置

    proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回 收,超时的销毁 --&gt; &lt;house-keeping-sleep-time&gt;90000&lt;/house-keeping-sleep-time&gt; - &lt;!-- 指因未有空闲连接可以分配而在队列中...

    写一个java程序实现数据库连接池

    46.请写一个java程序实现数据库连接池功能? 用一个自己的类实现Connection接口太麻烦...主要是连接(Connection)的close()方法,从连接池得到的是一个代理对象,过滤Connection的close()方法。你还能找到其他方法吗?

    ftp连接池实例

    写ftp连接池的目的就是合理的利用资源,本文的目的是在初始的时候,创建10个Ftp连接,放到一个队列中去,当多个用户同时去下载ftp上的文件的时候,就会从队列中取,若当前的队列中存在着空闲的连接,就获取该ftp的...

    手写数据库连接池(java)连sqlserver

    发现csdn上的连接池都是配设xml的,就手写了一份数据库连接池(java),连接sqlserver,里面一共两个java代码,Conn类包含了Connection和标志位,myconnection包含了数据库连接池的使用:获取连接,增加连接,释放...

    Java 的JDBC 数据库连接池实现方法.zip_J2EE JSP_connection pool jdbc_java 连接池

    J2EE 程序员一般都有现成的应用服务器所带的JDBC 数据库连接池,不过对于开发一般的 Java Application 、 Applet 或者 JSP、velocity 时,我们可用的JDBC 数据库连接池并不多,并且一般性能都不好。我们可以自己写一...

    R2高性能数据库连接池v1.5源码

    conn用完后必须关闭,不然池中连接会被用完(原理:关闭conn时放回池中)。 多池调用方式为R2Pool pool1=R2PoolUtil.getPool(new File("第一个连接池的配置文件.properties")); Connnection conn1 = pool1.get...

    hibernate连接池.doc

    Hibernate支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以及DBCP 在配置连接池时需要注意的有三点: 一、Apche的DBCP在Hibernate2中受支持,但在Hibernate3中已经不再推荐使用,官方的解释是这个连接池存在...

Global site tag (gtag.js) - Google Analytics