java_jdbc_基本连接池

来源:互联网 发布:java json tobean 编辑:程序博客网 时间:2024/06/06 05:57

MyDataSource实现封装连接池,MyConnection实现connection类,通过代理模式相互调用

 

package cn.itcast;import java.sql.*;public class ConnectDemo {public static void main(String[] args) throws Exception {for(int i=0;i<21;i++){Connection conn = JdbcUtils.getConnection();System.out.println(conn);JdbcUtils.free(null, null, conn);}}}


 

 

 

package cn.itcast;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import cn.itcast.datasource.DataSourceDemo1;public final class JdbcUtils {// private static String url = "jdbc:mysql://";// private static String user = "";// private static String password = "";private static MyDataSource mydatasource = null;public JdbcUtils() {}static {try {Class.forName("com.mysql.jdbc.Driver");mydatasource = new MyDataSource();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockthrow new ExceptionInInitializerError(e);}}public static Connection getConnection() throws SQLException {return mydatasource.getConnection();}public static void free(ResultSet rs, Statement st, Connection conn) {try {if (rs != null)rs.close();} catch (SQLException e) {e.printStackTrace();} finally {try {if (st != null)st.close();} catch (SQLException e) {e.printStackTrace();} finally {if (conn != null)try {conn.close();//mydatasource.free(conn);} catch (Exception e) {e.printStackTrace();}}}}}


 

 

 

package cn.itcast;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.LinkedList;public class MyDataSource {private static String url = "jdbc:mysql://";private static String user = "";private static String password = "";// 初始化连接数private static int initCount = 5;//最大创建数private static int maxCount = 10;int currentCount = 0;public LinkedList<Connection> connectionsPool = new LinkedList<Connection>();public MyDataSource() {for (int i = 0; i < initCount; i++) {try {this.connectionsPool.addLast(this.createConnection());this.currentCount++;} catch (SQLException e) {throw new ExceptionInInitializerError(e);}}}public Connection getConnection() throws SQLException {synchronized (connectionsPool) {if (this.connectionsPool.size() > 0) {return this.connectionsPool.removeFirst();} else if (this.currentCount < maxCount) {this.currentCount++;return this.createConnection();} else {throw new SQLException("超过最大连接数量");}}}public void free(Connection conn) {this.connectionsPool.addLast(conn);}private Connection createConnection() throws SQLException {Connection realConn = DriverManager.getConnection(url, user, password);MyConnection myConnection = new MyConnection(realConn, this);return myConnection;}}


 

 

package cn.itcast;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 cn.itcast.datasource.DataSourceDemo1;public class MyConnection implements Connection {private Connection realConnection;private MyDataSource dataSource;//连接最大使用次数private int maxUseCount = 10;//当前使用次数private int currentUserCount = 0;MyConnection(Connection connection, MyDataSource datasource) {this.realConnection = connection;this.dataSource = datasource;}@Overridepublic void close() throws SQLException {// TODO Auto-generated method stubthis.currentUserCount++;if (this.currentUserCount > this.maxUseCount)this.dataSource.connectionsPool.addLast(this);else {this.realConnection.close();// 真正关掉this.dataSource.currentCount--;}}@Overridepublic <T> T unwrap(Class<T> iface) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic Statement createStatement() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String sql) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic CallableStatement prepareCall(String sql) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic String nativeSQL(String sql) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void setAutoCommit(boolean autoCommit) throws SQLException {// TODO Auto-generated method stub}@Overridepublic boolean getAutoCommit() throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic void commit() throws SQLException {// TODO Auto-generated method stubthis.realConnection.commit();}@Overridepublic void rollback() throws SQLException {// TODO Auto-generated method stub}@Overridepublic boolean isClosed() throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic DatabaseMetaData getMetaData() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void setReadOnly(boolean readOnly) throws SQLException {// TODO Auto-generated method stub}@Overridepublic boolean isReadOnly() throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic void setCatalog(String catalog) throws SQLException {// TODO Auto-generated method stub}@Overridepublic String getCatalog() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void setTransactionIsolation(int level) throws SQLException {// TODO Auto-generated method stub}@Overridepublic int getTransactionIsolation() throws SQLException {// TODO Auto-generated method stubreturn 0;}@Overridepublic SQLWarning getWarnings() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void clearWarnings() throws SQLException {// TODO Auto-generated method stubthis.realConnection.clearWarnings();}@Overridepublic Statement createStatement(int resultSetType, int resultSetConcurrency)throws SQLException {// TODO Auto-generated method stubreturn this.realConnection.createStatement();}@Overridepublic PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic CallableStatement prepareCall(String sql, int resultSetType,int resultSetConcurrency) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Map<String, Class<?>> getTypeMap() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void setTypeMap(Map<String, Class<?>> map) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setHoldability(int holdability) throws SQLException {// TODO Auto-generated method stub}@Overridepublic int getHoldability() throws SQLException {// TODO Auto-generated method stubreturn 0;}@Overridepublic Savepoint setSavepoint() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Savepoint setSavepoint(String name) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void rollback(Savepoint savepoint) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void releaseSavepoint(Savepoint savepoint) throws SQLException {// TODO Auto-generated method stub}@Overridepublic Statement createStatement(int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic CallableStatement prepareCall(String sql, int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String sql, int[] columnIndexes)throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String sql, String[] columnNames)throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Clob createClob() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Blob createBlob() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic NClob createNClob() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic SQLXML createSQLXML() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean isValid(int timeout) throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic void setClientInfo(String name, String value)throws SQLClientInfoException {// TODO Auto-generated method stub}@Overridepublic void setClientInfo(Properties properties)throws SQLClientInfoException {// TODO Auto-generated method stub}@Overridepublic String getClientInfo(String name) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Properties getClientInfo() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Array createArrayOf(String typeName, Object[] elements)throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Struct createStruct(String typeName, Object[] attributes)throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void setSchema(String schema) throws SQLException {// TODO Auto-generated method stub}@Overridepublic String getSchema() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void abort(Executor executor) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setNetworkTimeout(Executor executor, int milliseconds)throws SQLException {// TODO Auto-generated method stub}@Overridepublic int getNetworkTimeout() throws SQLException {// TODO Auto-generated method stubreturn 0;}}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 0