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个连接对象

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

  1. PyQt学习随笔:通过自定义类重写QApplication的notify方法捕获应用的所有消息

    PyQt程序通过调用QApplication类的exec_()(sys.exit(app.exec_()) 进入程序主循环,开始处理事件,它从事件队列中获取本地窗口系统事件,将它们转化为 QEvent ...

  2. 存储过程 务的概念 事务的特性 关于异常的处理 连接池 构JdbcUtil类

    1 存储过程    1)用当地数据库语言,写的一段业务逻辑算法,并该算法存储在客户端    2)使用存储过程需要用于CallableStatement接口,同时需要使如下SQL命令调用:{call a ...

  3. 在jdbc基础上进阶一小步的C3p0 连接池(DBCP 不能读xml配置文件,已淘汰) 和DBUtils 中两个主要类QueryRunner和ResultSetHandler的使用

    首先看C3p0这个连接池,最大优势可以自动读取默认的配置文件 <?xml version="1.0" encoding="UTF-8"?> < ...

  4. Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!

    Go/Python/Erlang编程语言对比分析及示例   本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...

  5. JDBC----数据库连接池(connection pool)

    •数据库连接池的基本思想就是为数据库连接建立一个"缓冲池".预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从"缓冲池"中取出一个,使用完毕之后再 ...

  6. 连接池(Connection Pool)技术

    解释: 连接池(Connection Pool)技术的核心思想是:连接复用,通过建立一个数据库连接池以及一套连接使用.分配.管理策略,使得该连接池中的连接可以得到高效.安全的复用,避免了数据库连接频繁 ...

  7. 类的本质、description方法、SEL、NSLog输出增强

    一.类的本质 1.类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class *Class; 类名就代表着类对象 ...

  8. OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法

    一. 分类-Category 1. 基本用途:Category  分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...

  9. Java中增强一个类的几种方法

    今天有人问我怎么增强一个类的功能.博客刚好没东西,今天就讲讲增强类. 增强的手段有三种类型: 1.继承或者实现接口:特点是被增强对象不能变,增强的内容不能变. 2.装饰着模式:特点是被增强对象可变,但 ...

随机推荐

  1. [Functional Programming] Capture Side Effects in a Task / Async

    We examine the data structure Task, see some constructors, familiar methods, and finally how it capt ...

  2. double转成string时,不以科学计数法表示

    用gson解析json串的时候,经常会自动把我想用string表示的内容转换成double,并且还是科学计数法,这一点也不科学,写个方法,格式化一下. public static String par ...

  3. WCF 之 消息契约(MessageContract)

    对于SOAP来说主要由两部分构成Header和Body,他们两个共同构成了SOAP的信封,通常来说Body保存具体的数据内容,Header保存一些上下文信息或关键信息. 比如:在一些情况下,具有这样的 ...

  4. HDoj-2095-与众不同

    Problem Description In the new year party, everybody will get a "special present".Now it's ...

  5. How to add a button in the seletions "More"

    <?xml version="1.0" encoding="utf-8"?> <openerp> <data> <re ...

  6. TensorFlow编译androiddemo

    首先是把tensorflow克隆到本地一份. git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git 既 ...

  7. TCP 中的Push flag 的作用

    发送方使用该标志通知接收方将所收到的数据全部提交给接收进程.这里的数据包括接收方已经接收放在接收缓存的数据和刚刚收到的PUSH位置一的TCP报文中封装的应用数据.还是看一个简单明了的图示吧:

  8. OFBiz:处理nextRequestResponse

    这里的nextRequestResponse是指RequestHandler中doRequest()函数在最后使用的一个变量,doRequest()会依据nextRequestResponse返回不同 ...

  9. 基于olami开放语义平台的微信小程序遥知之源码实现

    概述 实现一个智能生活信息查询的小秘书功能,支持查天气.新闻.日历.汇率.笑话.故事.百科.诗词.邮编.区号.菜谱.股票.节目预告,还支持闲聊.算24点.数学计算.单位换算.购物.搜索等功能. 使用方 ...

  10. webqq协议分析之~~~~登陆

    最近好几个新项目积一起了,比较忙,所以博客迟迟未更新,还请各位见谅!下面来继续分析webqq协议,本章将说明如何实现登陆 1:输入QQ号和密码登陆,检测HTTP请求url如下,这是第一次登陆 http ...