数据库连接池
1、连接池原理:(面试)
目的:解决建立数据库连接耗费资源和时间很多的问题,提高性能。
2、编写标准的数据源
自定义数据库连接池要实现javax.sql.DataSource接口,一般都叫数据源。
//自定义的连接池,实现了标准的规范,DataSource接口
public class MyDataSource implements DataSource {
private static LinkedList<Connection> pool = (LinkedList<Connection>) Collections.synchronizedList(new LinkedList<Connection>());
static{
for(int i=1;i<=10;i++){
try {
Connection conn = DBUtil.getConnection();
pool.add(conn);
} catch (SQLException e) {
throw new ExceptionInInitializerError("连接池初始化出事儿");
}
}
}
//从池子中取出一个Connection对象并返回的方法
public Connection getConnection() throws SQLException {
Connection conn = null;
//判断池子是否为空
if(pool.size()>0){
conn = pool.removeFirst();//从池子中删除并返回第一个Connection对象
MyConnection con = new MyConnection(conn,pool);
return con;
}else{
//创建新的Connection对象
throw new RuntimeException("服务器忙。。。。");
}
}
public PrintWriter getLogWriter() throws SQLException {
return null;
}
public void setLogWriter(PrintWriter out) throws SQLException {}
public void setLoginTimeout(int seconds) throws SQLException {}
public int getLoginTimeout() throws SQLException {
return 0;
}
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
public Connection getConnection(String username, String password)
throws SQLException {
return null;
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
3、编写数据源时遇到的问题及解决办法
a、装饰设计模式:使用频率很高
目的:改写已存在的类的某个方法或某些方法,装饰设计模式(包装模式)
口诀:
1、编写一个类,实现与被包装类相同的接口。(具备相同的行为)
2、定义一个被包装类类型的变量。
3、定义构造方法,把被包装类的对象注入,给被包装类变量赋值。
4、对于不需要改写的方法,调用原有的方法。
5、对于需要改写的方法,写自己的代码。
使用装饰设计模式,改变close方法的功能
(装饰设计模式在之前的文章提到过(http://blog.csdn.net/daliyuan350649623/article/details/52334087))
//使用装饰设计模式,改变close方法的功能
public class MyConnection implements Connection {
private Connection conn;
private LinkedList<Connection> pool;
public MyConnection(Connection conn, LinkedList<Connection> pool) {
super();
this.conn = conn;
this.pool = pool;
}
public void close() throws SQLException {
pool.addLast(conn);
}
public <T> T unwrap(Class<T> iface) throws SQLException {
return conn.unwrap(iface);
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
public Statement createStatement() throws SQLException {
return conn.createStatement();
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
return conn.prepareStatement(sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
return null;
}
public String nativeSQL(String sql) throws SQLException {
return null;
}
public void setAutoCommit(boolean autoCommit) throws SQLException {}
public boolean getAutoCommit() throws SQLException {
return false;
}
public void commit() throws SQLException {}
public void rollback() throws SQLException {}
public boolean isClosed() throws SQLException {
return false;
}
public DatabaseMetaData getMetaData() throws SQLException {
return null;
}
public void setReadOnly(boolean readOnly) throws SQLException {}
public boolean isReadOnly() throws SQLException {
return false;
}
public void setCatalog(String catalog) throws SQLException {}
public String getCatalog() throws SQLException {
return null;
}
public void setTransactionIsolation(int level) throws SQLException {}
public int getTransactionIsolation() throws SQLException {
return 0;
}
public SQLWarning getWarnings() throws SQLException {
return null;
}
public void clearWarnings() throws SQLException {}
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
return null;
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
return null;
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
return null;
}
public Map<String, Class<?>> getTypeMap() throws SQLException {
return null;
}
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {}
public void setHoldability(int holdability) throws SQLException {}
public int getHoldability() throws SQLException {
return 0;
}
public Savepoint setSavepoint() throws SQLException {
return null;
}
public Savepoint setSavepoint(String name) throws SQLException {
return null;
}
public void rollback(Savepoint savepoint) throws SQLException {}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {}
public Statement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return null;
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return null;
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return null;
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
return null;
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
throws SQLException {
return null;
}
public PreparedStatement prepareStatement(String sql, String[] columnNames)
throws SQLException {
return null;
}
public Clob createClob() throws SQLException {
return null;
}
public Blob createBlob() throws SQLException {
return null;
}
public NClob createNClob() throws SQLException {
return null;
}
public SQLXML createSQLXML() throws SQLException {
return null;
}
public boolean isValid(int timeout) throws SQLException {
return false;
}
public void setClientInfo(String name, String value)
throws SQLClientInfoException {}
public void setClientInfo(Properties properties)
throws SQLClientInfoException {}
public String getClientInfo(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Properties getClientInfo() throws SQLException {
return null;
}
public Array createArrayOf(String typeName, Object[] elements)
throws SQLException {
return null;
}
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
return null;
}
public void setSchema(String schema) throws SQLException {}
public String getSchema() throws SQLException {
return null;
}
public void abort(Executor executor) throws SQLException {}
public void setNetworkTimeout(Executor executor, int milliseconds)
throws SQLException {}
public int getNetworkTimeout() throws SQLException {
return 0;}
}