同过增强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个连接对象

原文地址:https://www.cnblogs.com/jinhengyu/p/8321472.html