package cn.itcast.jdbc.datasourse;
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.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
//通过代理模式解决用户关闭链接的习惯
public class MyConnection implements Connection { //这里实现的是java.sql.Connection
private Connection realConnection; //这里的Connection也是java.sql.Connection
private MyDataSourse2 myDataSourse; //链接在MyDataSource中存放,所以这里要有MyDataSouce
/***********************************************************************************************************************************/
/********* //我们可以限制使用次链接的次数,当该链接使用超过多少次之后就将该链接真正的关掉 ******/
/********* private int maxUseCount = 10; //限制最大使用次数 ******/
/********* private int currentUseCount = 0; //记录当前使用次数 ******/
/********* 这一块可以不要,当你有这个需求的时候可以加上,此时close方法可以改写为下面的样式 ******/
/************************************************************************************************************************************/
/********* @Override ******/
/********* public void close() throws SQLException { ******/
/********* this.currentUseCount++; ******/
/********* if(this.currentUseCount < maxUseCount) ******/
/********* this.myDataSourse.connectionsPool.addLast(this);//这里只能用this也就是MyConnection ******/
/********* else ******/
this.realConnection.close(); ******/
}
/*************************************************************************************************************************8***********/
MyConnection(Connection connection,MyDataSourse2 myDataSourse){
this.realConnection = connection;
this.myDataSourse = myDataSourse;
}
//这里实现了java.sql.Connection接口,我们只需要处理close方法,让他当被调用时不是直接关掉conn而是将链接放回到连接池中
@Override
public void close() throws SQLException {
this.myDataSourse.connectionsPool.addLast(this); //这里只能用this也就是MyConnection
}
//////////////////////////下面的方法可以简单的处理。我们重点看上面的部分//////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return this.realConnection.unwrap(iface);
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return this.realConnection.isWrapperFor(iface);
}
@Override
public Statement createStatement() throws SQLException {
return this.realConnection.createStatement();
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return this.realConnection.prepareStatement(sql);
}
@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 cn.itcast.jdbc.datasourse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
public class MyDataSourse2 {
private static String url = "jdbc:mysql://localhost:3306/test";
private static String user = "root";
private static String pwd = "";
//用于存放链接
LinkedList<Connection> connectionsPool = new LinkedList<Connection>();
/*当创建链接时,不是随意的我想创建多少个链接就创建多少个链接,这时因为数据库的链接是有限的
* 当超过这个范围时,数据库是承受不了的,所以我们就要限制创建链接的个数*/
private static int initCount = 5; //设置最小链接数
private static int maxCount = 20; //设置最大链接数
private int currentCount = 0; //记录当前链接数
//初始化链接池时,向链接池内放入10个链接
public MyDataSourse2(){
try {
for(int i = 0;i< initCount ;i++){
this.connectionsPool.addLast(this.createConnection());
this.currentCount++;
}
} catch (SQLException e) {
// e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
//得到链接
/*Connection是不支持多线程的,当有多个请求建立链接时,就要保证他们各自拿到的链接不相等
* 这时就需要用到同步代码块*/
public Connection getConnection() throws SQLException{
synchronized(connectionsPool){ //这里的锁用的就是链接池
if(this.connectionsPool.size() > 0){ //如果池里还有就直接取
return this.connectionsPool.removeFirst();
}
if(this.currentCount < maxCount) { //如果池里没有了,就判断当前链接是否超过最大链接数,如果没有就创建
this.currentCount++;
return this.createConnection();
}
/*如果说池里没有了,而且已经达到最大链接数这时有3种处理方式
* 1 抛一个异常出去
* 2 返回一个null
* 3 让它等待别人释放链接
* 这里简单的处理,抛一个异常出去
* */
throw new SQLException("已没有链接");
}
}
//释放链接 如果用链接池的话,当我们要释放资源的时候就不能够简单的把conn给关闭掉,而应该是把conn重新放回到链接池中
public void free(Connection conn){
this.connectionsPool.addLast(conn);
}
//创建链接
private Connection createConnection() throws SQLException{
// return DriverManager.getConnection(url,user,pwd);
Connection realConn = DriverManager.getConnection(url,user,pwd);
MyConnection myConnection = new MyConnection(realConn, this);
return myConnection;
}
}
/*
* 当我们对数据库进行操作时,往往花费在建立和数据库的链接时花费的时间最长,所以最好就是链接保证链接不要
* 频繁的建立,这时就应该考虑到创建一个链接池,每次用的时候直接从链接池中去取,用完后再放回去
* 这样做虽然在创建时花费的时间会很长,但是一旦创建完成,在用的时候就能够很好的提高效率
* */