`
webdev2014
  • 浏览: 678528 次
文章分类
社区版块
存档分类
最新评论

数据源

 
阅读更多

package edu.sdkd.datasource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;

import edu.sdkd.utils.Utils;

public class MyDataSource {

private static Properties properties = Utils.readProperties("db.properties");

private static String driver = properties.getProperty("driver");
private static String url = properties.getProperty("url");
private static String user = properties.getProperty("user");
private static String password = properties.getProperty("password");

private static int initCount = Integer.valueOf(properties.getProperty("initCount"));
private static int maxCount = Integer.valueOf(properties.getProperty("maxCount"));
private static int currentCount = Integer.valueOf(properties.getProperty("currentCount"));

LinkedList<Connection> connectionsPool = new LinkedList<Connection>();

{
for (int i = 0; i < initCount; i++) {
try {
this.connectionsPool.addLast(this.createConnection());
this.currentCount++;
} catch (SQLException e) {
throw new ExceptionInInitializerError();
}
}
}

static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
new RuntimeException("加载驱动失败");
}
}

private static final MyDataSource myDataSource = new MyDataSource();

private MyDataSource() {

}

public static MyDataSource getMyDataSource() {
return myDataSource;
}

public Connection getConnection() throws SQLException {
synchronized (connectionsPool) {
if (this.connectionsPool.size() > 0)
return this.connectionsPool.removeFirst();
if (this.currentCount < this.maxCount) {
this.currentCount++;
return this.createConnection();
}
throw new SQLException("已没有连接");
}
}

public void free(Connection conn) {
this.connectionsPool.addLast(conn);
}

private Connection createConnection() throws SQLException {
Connection realConn = DriverManager.getConnection(url, user, password);
MyConnectionHandler proxy = new MyConnectionHandler(this);
return proxy.bind(realConn);

}
}

--------------------------------------------------------------------------

package edu.sdkd.datasource;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;

class MyConnectionHandler implements InvocationHandler {

private Connection realConnection;
private Connection warpedConnection;
private MyDataSource dataSource;

MyConnectionHandler(MyDataSource dataSource) {
this.dataSource = dataSource;
}

Connection bind(Connection realConn) {
this.realConnection = realConn;
this.warpedConnection = (Connection)Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class[] { Connection.class }, this);
return warpedConnection;
}

public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("close".equals(method.getName())){
this.dataSource.connectionsPool.addLast(this.warpedConnection);
}
return method.invoke(this.realConnection, args);
}

}

-------------------------------------------------------------------------------------------------------------

package edu.sdkd.datasource;

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;

public class MyConnection implements Connection{
private Connection realConnection;
private MyDataSource dataSource;

MyConnection(Connection realConnection, MyDataSource dataSource){
this.realConnection = realConnection;
this.dataSource = dataSource;
}

public void clearWarnings() throws SQLException {
this.realConnection.clearWarnings();
}

public void close() throws SQLException {
this.dataSource.connectionsPool.addLast(this);
}

public void commit() throws SQLException {
this.realConnection.commit();
}

public Array createArrayOf(String typeName, Object[] elements)
throws SQLException {
return this.realConnection.createArrayOf(typeName, elements);
}

public Blob createBlob() throws SQLException {
return this.realConnection.createBlob();
}

public Clob createClob() throws SQLException {
return this.realConnection.createClob();
}

public NClob createNClob() throws SQLException {
return this.realConnection.createNClob();
}

public SQLXML createSQLXML() throws SQLException {
return this.realConnection.createSQLXML();
}

public Statement createStatement() throws SQLException {
return this.realConnection.createStatement();
}

public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
return this.realConnection.createStatement();
}

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

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}

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

}

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

}

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

}

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

}

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

}

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

}

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

}

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

}

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

}

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

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

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

}

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

}

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

}

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

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

-------------------------------------------------------------------------------------------

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics