JDBC(连接池) -- 02(II)
自定义连接池强化close方法 使用装饰者模式
1.我们想增强Connection里的close方法由于我们使用的装饰模式故写一个MyConnection实现接口Connection
2.实现一个构造方法。为什么要这个构造方法?因为我们要增强close方法。增强的对象是怎么过来的呢?传参过来的。构造方法就是这个作用。(装饰模式的固定写法)
3.书写我们需要增强的方法close.把关闭操作改成归还到连接池中
4.在MyDataSource中进行装饰conn = JDBCUtils.getConnection(); MyConnection myconn = new MyConnection(conn,pool); 把myconn添加到集合中 pool.add(myconn);
5.书写测试类。测试类中释放资源直接调用工具类JDBCUtils.Release();此时的--- conn.close();//这时的conn值是myconn。调用的是MyConnection中的close方法。
重点:我们重新实现了Connection 然后conn.prexxx就需要我们重写了。不重写方法他永远是return null,所以需要我们重写下。
记得吗?我们的构造方法的参Connection conn; 我们这样做:return conn.prepareStatement(sql); 不就是Connection类中未被
改写的方法吗。
我们需要三个类:
1.MyConnection 实现Conneciton接口
2.MyDataSource 实现DataSource接口
3.TestMyDataSource2 测试
1.MyConnection方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.it.JDBC; 2 3 import java.sql.Array; 4 import java.sql.Blob; 5 import java.sql.CallableStatement; 6 import java.sql.Clob; 7 import java.sql.Connection; 8 import java.sql.DatabaseMetaData; 9 import java.sql.NClob; 10 import java.sql.PreparedStatement; 11 import java.sql.SQLClientInfoException; 12 import java.sql.SQLException; 13 import java.sql.SQLWarning; 14 import java.sql.SQLXML; 15 import java.sql.Savepoint; 16 import java.sql.Statement; 17 import java.sql.Struct; 18 import java.util.LinkedList; 19 import java.util.Map; 20 import java.util.Properties; 21 22 public class MyConnection implements Connection { 23 private static LinkedList<Connection> pool; 24 private static Connection conn; 25 //构造方法,传入真实对象的引用。 26 public MyConnection(LinkedList<Connection> pool,Connection conn){ 27 MyConnection.pool = pool; 28 MyConnection.conn = conn; 29 } 30 @Override 31 //归还连接到池子中 32 public void close() throws SQLException { 33 pool.add(conn); 34 System.out.println(pool.size()+":连接已归还"); 35 } 36 37 @Override 38 //不重写会出现空指针异常。prepareStatement永远return null 39 public PreparedStatement prepareStatement(String sql) throws SQLException { 40 return conn.prepareStatement(sql); 41 } 42 xxxxxxxxxxxxxxxxxxxx省略其他代码
2.MyDataSource2方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.it.JDBC; 2 3 import java.io.PrintWriter; 4 import java.sql.Connection; 5 import java.sql.SQLException; 6 import java.util.LinkedList; 7 8 import javax.sql.DataSource; 9 10 public class MyDataSource2 implements DataSource { 11 private static LinkedList<Connection> pool = new LinkedList<Connection>(); 12 private static Connection conn; 13 //静态代码块初始化连接数 14 static{ 15 for(int i=0;i<5;i++){ 16 conn = JDBCUtils.getConnection(); 17 //开始装饰 18 MyConnection my = new MyConnection(pool, conn); 19 pool.add(my); 20 } 21 } 22 23 @Override 24 //获取池子中的连接 25 public Connection getConnection() throws SQLException { 26 conn = pool.remove(0); 27 return conn; 28 } 29 30 @Override 31 public PrintWriter getLogWriter() throws SQLException { 32 // TODO Auto-generated method stub 33 return null; 34 } 35 36 @Override 37 public void setLogWriter(PrintWriter out) throws SQLException { 38 // TODO Auto-generated method stub 39 40 } 41 42 @Override 43 public void setLoginTimeout(int seconds) throws SQLException { 44 // TODO Auto-generated method stub 45 46 } 47 48 @Override 49 public int getLoginTimeout() throws SQLException { 50 // TODO Auto-generated method stub 51 return 0; 52 } 53 54 @Override 55 public <T> T unwrap(Class<T> iface) throws SQLException { 56 // TODO Auto-generated method stub 57 return null; 58 } 59 60 @Override 61 public boolean isWrapperFor(Class<?> iface) throws SQLException { 62 // TODO Auto-generated method stub 63 return false; 64 } 65 66 @Override 67 public Connection getConnection(String username, String password) 68 throws SQLException { 69 // TODO Auto-generated method stub 70 return null; 71 } 72 73 }
3.TestMyDataSource2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.it.TestJDBC; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import org.junit.Test; 7 import com.it.JDBC.JDBCUtils; 8 import com.it.JDBC.MyDataSource2; 9 10 public class TestMyDataSource2 { 11 @Test 12 public void selectById() { 13 Connection conn= null; 14 PreparedStatement psmt = null; 15 ResultSet rs = null; 16 try { 17 MyDataSource2 mds2 = new MyDataSource2(); 18 conn = mds2.getConnection(); 19 String sql = "select * from t_user where id=?"; 20 psmt = conn.prepareStatement(sql); 21 psmt.setInt(1, 2015008); 22 rs = psmt.executeQuery(); 23 while(rs.next()){ 24 System.out.println(rs.getInt("id")); 25 } 26 } catch (Exception e) { 27 e.printStackTrace(); 28 }finally{ 29 JDBCUtils.Release(conn, psmt, rs); 30 } 31 } 32 }