数据库连接池
池参数(所有池参数都有默认值)
初始大小:10个
最小空闲连接数:3
增量:一次创建的最小单位(5个)
最大空闲连接数:12个
最大连接数:20个
最大的等待时间:1000毫秒
四大连接参数
连接池也是使用四大连接参数来完成创建连接对象
实现的接口
连接池必须实现:javax.sql.DataSource接口
连接池返回的Connection对象,它的close()方法与众不同。调用它的close()不是关闭,而是把连接归还给池
1、数据库连接池的概念
用池来管理Connection,这可以重复使用Connection,有了池,所以我们就不用自己来创建Connection,
而是通过池来获取Connection对象,当用完Connection后,调用Connection的close() 方法也不会真正的关闭Connection,
而是把Connection"归还"给池,池就可以
对象的增强手段
继承
被增强的对象固定
增强的内容也是固定的
装饰者模式
被增强的对象是可以切换的
增强的内容是固定的
动态代理(AOP)
被增强的内容可以切换
增强的内容也可以切换
继承
缺点:
1、增强的内容是死的,不能动
2、被增强的对象也是死的
使用继承会使类增多
class 咖啡类{}
class 有糖咖啡 extends 咖啡类{}
class 加奶咖啡 extends 咖啡类{}
class 加盐咖啡 extends 咖啡类{}
class 加醋咖啡 extends 咖啡类{}
class 加糖加奶 extends 加奶{}
-------------------------------------
装饰者模式
1、增强的内容是不能修改的
2、被增强的对象可以是任意的
class 咖啡类{}
class 有糖咖啡 extends 咖啡类{}
class 加奶咖啡 extends 咖啡类{}
class 加盐咖啡 extends 咖啡类{}
class 加醋咖啡 extends 咖啡类{}
class 加糖加奶 extends 加奶{}
咖啡 a = new 加糖();
咖啡 b = new 加盐(a);//对a进行装饰:就是给a加盐
咖啡 c = new 加奶(b);
Java API
IO流:
四大家:
1、字节:InputStream、OutputStream
2、字符:Reader、Writer
InputStream
FileInputStream:是节点流:就是和一个资源绑定在一起的。文件
BufferedInputStream:是装饰流。创建一定要给一个底层对象,不管是什么流,都会添加缓冲区
new BufferedInputStream(任意的InputStream)
FileInputStream in = new FileInputStream("F:a.jpg");
BufferedInputStream b = new BufferedInputStream(in);
ObjectInputStream o = new ObjectInputStream(b);
但不知道被增强的具体类 所以不能用继承
Connection con = ...
class MyConnection extends XXXConnection{
public void close(){
归还给池;
}
}
装饰:不知道被增强对象的具体类型时,可以使用
1、是你还是你,一切拜托你。
is a
has a
use a
class MyConnection implements Connection{//是你
//还是你
private Connection con;//底层对象,被增强的对象
public MyConnection(Connection con){//通过构造器传递底层对象
this.con = con;
}
//一切拜托你
public Statement createStatement(){
return con.createStatement();
}
//增强点
public void close(){
把当前连接归还给池;
}
}
Connection con = 通过四大参数创建连接对象。由MySQL提供的
Connection con1 = new MyConnection(con);
con1.createStatement();
con.createStatement();
些许不同 主要还是靠底层类
con1.close();
con.close();
所需的jar包
public class Demo1 {
//需要 mysql connector| pool logging dbcp jar包
@Test
public void fun1() throws SQLException{
/*
* 1、创建连接池对象
* 2、配置四大参数
* 3、配置池参数
* 4、得到连接对象
*/
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/db1");
dataSource.setUsername("root");
dataSource.setPassword("123");
dataSource.setMaxTotal(20);
dataSource.setMinIdle(3);
dataSource.setMaxWaitMillis(1000);
Connection con = dataSource.getConnection();
Connection con1 = new MyConnection(con);
System.out.println(con1.getClass().getName());
/**
* 连接池内部使用四大参数创建了连接对象。即Mysql驱动提供的Connection
* 连接池使用mysql的连接对象进行了装饰,只对close()方法进行了增强
* 装饰之后的Connection的close()方法,用来把当前连接归还给池
*/
con1.close();//把连接归还给池
}
}
class MyInputStream extends InputStream{
private InputStream in;
private int key;
public MyInputStream(InputStream in,int key){
this.in = in;
this.key = key;
}
//文件加密 需要获取key才能反向还原
@Override
public int read() throws IOException{
return this.in.read()-key;
}
}
MyConnection
public class MyConnection extends MyWrapper{
public MyConnection(Connection con) {
super(con);
}
public void close() throws SQLException{
System.out.println("哈哈");
super.close();
}
}
package my.jdbc;
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 MyWrapper implements Connection{
private Connection con;
public MyWrapper(Connection con){
this.con=con;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return con.unwrap(iface);
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return con.isWrapperFor(iface);
}
@Override
public Statement createStatement() throws SQLException {
return con.createStatement();
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return con.prepareStatement(sql);
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
return con.prepareCall(sql);
}
@Override
public String nativeSQL(String sql) throws SQLException {
return con.nativeSQL(sql);
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
con.setAutoCommit(autoCommit);
}
@Override
public boolean getAutoCommit() throws SQLException {
return con.getAutoCommit();
}
@Override
public void commit() throws SQLException {
con.commit();
}
@Override
public void rollback() throws SQLException {
con.rollback();
}
@Override
public void close() throws SQLException {
con.close();
}
@Override
public boolean isClosed() throws SQLException {
return con.isClosed();
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return con.getMetaData();
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
con.setReadOnly(readOnly);
}
@Override
public boolean isReadOnly() throws SQLException {
return con.isReadOnly();
}
@Override
public void setCatalog(String catalog) throws SQLException {
con.setCatalog(catalog);
}
@Override
public String getCatalog() throws SQLException {
return con.getCatalog();
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
con.setTransactionIsolation(level);
}
@Override
public int getTransactionIsolation() throws SQLException {
return con.getTransactionIsolation();
}
@Override
public SQLWarning getWarnings() throws SQLException {
return con.getWarnings();
}
@Override
public void clearWarnings() throws SQLException {
con.clearWarnings();
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return con.createStatement(resultSetType, resultSetConcurrency);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
return con.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return con.prepareCall(sql, resultSetType, resultSetConcurrency);
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
return con.getTypeMap();
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
con.setTypeMap(map);
}
@Override
public void setHoldability(int holdability) throws SQLException {
con.setHoldability(holdability);
}
@Override
public int getHoldability() throws SQLException {
return con.getHoldability();
}
@Override
public Savepoint setSavepoint() throws SQLException {
return con.setSavepoint();
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
return con.setSavepoint(name);
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
con.rollback(savepoint);
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
con.releaseSavepoint(savepoint);
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return con.createStatement(resultSetType, resultSetConcurrency,resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return con.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return con.prepareCall(sql, resultSetType, resultSetConcurrency,resultSetHoldability);
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return con.prepareStatement(sql, autoGeneratedKeys);
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return con.prepareStatement(sql, columnIndexes);
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return con.prepareStatement(sql,columnNames);
}
@Override
public Clob createClob() throws SQLException {
return con.createClob();
}
@Override
public Blob createBlob() throws SQLException {
return con.createBlob();
}
@Override
public NClob createNClob() throws SQLException {
return con.createNClob();
}
@Override
public SQLXML createSQLXML() throws SQLException {
return con.createSQLXML();
}
@Override
public boolean isValid(int timeout) throws SQLException {
return con.isValid(timeout);
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
con.setClientInfo(name, value);
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
con.setClientInfo(properties);
}
@Override
public String getClientInfo(String name) throws SQLException {
return con.getClientInfo(name);
}
@Override
public Properties getClientInfo() throws SQLException {
return con.getClientInfo();
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
return con.createArrayOf(typeName, elements);
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return con.createStruct(typeName, attributes);
}
@Override
public void setSchema(String schema) throws SQLException {
con.setSchema(schema);
}
@Override
public String getSchema() throws SQLException {
return con.getSchema();
}
@Override
public void abort(Executor executor) throws SQLException {
con.abort(executor);
}
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
con.setNetworkTimeout(executor, milliseconds);
}
@Override
public int getNetworkTimeout() throws SQLException {
return con.getNetworkTimeout();
}
}