自定义连接池(装饰者模式)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义连接池(装饰者模式)相关的知识,希望对你有一定的参考价值。

连接池概述:
  管理数据库的连接,
作用:
  提高项目的性能.
  就是在连接池初始化的时候存入一定数量的连接,用的时候通过方法获取,不用的时候归还连接即可.
  所有的连接池必须实现一个接口 javax.sql.DataSource接口
获取连接方法:
  Connection getConnection()
  归还连接的方法就是以前的释放资源的方法.调用connection.close();


 

增强方法:
  1.继承
  2.装饰者模式(静态代理)
  3.动态代理


 

装饰者模式:

  使用步骤:
    1.装饰者和被装饰者实现同一个接口或者继承同一个类
    2.装饰者中要有被装饰者的引用
    3.对需要增强的方法进行加强
    4.对不需要加强的方法调用原来方法


 

思路:把自定义的连接池close方法增强为放入连接池中

a.先自定义一个连接池

  1.先创建一个LinkedList集合(增删快,查询慢)作为连接池

  2.再在静态代码块中初始化连接池个数(3个)

  3.创建一个getConnection方法,判断连接池是否为空,然后执行下面操作

 1 package cn.jdbc_myConn;
 2 
 3 import java.sql.Connection;
 4 import java.util.LinkedList;
 5 
 6 import cn.jdbc_utils.JdbcUtils;
 7 
 8 public class MyDataSource {
 9     // 用LinkedList 增删快 查询慢
10     static LinkedList<Connection> pool = new LinkedList<>();
11     static {
12         // 初始化的时候需要放入3个连接,存放对象则是集合
13         for (int i = 0; i < 3; i++) {
14             try {
15                 Connection conn = JdbcUtils.getConnection();
16                 pool.addLast(conn);
17             } catch (Exception e) {
18                 e.printStackTrace();
19             }
20         }
21     }
22 
23     // 获取连接
24     public static Connection getConnection() {
25         if (pool.isEmpty()) {
26             for (int i = 0; i < 3; i++) {
27                 try {
28                     Connection conn = JdbcUtils.getConnection();
29                     pool.addLast(conn);
30                 } catch (Exception e) {
31                     e.printStackTrace();
32                 }
33             }
34         }
35         return pool.removeFirst();//先进先出
36     }
37     // 归还连接的方法用装饰者模式增强
38 }

使用装饰者模式增强close方法

1.创建一个MyConn类,实现Connection接口(java.sql)

2.需要增强的close方法则重写

3.不需要增强的方法调用原方法


自定义连接池类

技术分享
 1 package cn.jdbc_myConn;
 2 
 3 import java.sql.Connection;
 4 import java.util.LinkedList;
 5 
 6 import cn.jdbc_utils.JdbcUtils;
 7 
 8 public class MyDataSource {
 9     // 用LinkedList 增删快 查询慢
10     static LinkedList<Connection> pool = new LinkedList<>();
11     static {
12         // 初始化的时候需要放入3个连接,存放对象则是集合
13         for (int i = 0; i < 3; i++) {
14             try {
15                 Connection conn = JdbcUtils.getConnection();
16                 pool.addLast(conn);
17             } catch (Exception e) {
18                 e.printStackTrace();
19             }
20         }
21     }
22 
23     // 获取连接
24     public static Connection getConnection() {
25         if (pool.isEmpty()) {
26             for (int i = 0; i < 3; i++) {
27                 try {
28                     Connection conn = JdbcUtils.getConnection();
29                     pool.addLast(conn);
30                 } catch (Exception e) {
31                     e.printStackTrace();
32                 }
33             }
34         }
35         System.out.println("从池子中获取了一个连接");
36         Connection conn = pool.removeFirst();
37         MyConnWarp myConn = new MyConnWarp(conn,pool);
38         return myConn;//先进先出
39     }
40     // 归还连接的方法用装饰着模式增强
41 }
View Code

 

重写close方法类

技术分享
  1 package cn.jdbc_myConn;
  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 import java.util.concurrent.Executor;
 22 
 23 public class MyConnWarp implements Connection {
 24     
 25     private Connection conn;
 26     private LinkedList<Connection> list;
 27     //带参构造,传入connection,语句执行者,获取连接需要连接池
 28     public MyConnWarp(Connection conn){
 29         this.conn=conn;
 30     }
 31     public MyConnWarp(Connection conn,LinkedList<Connection> list){
 32         this.conn=conn;
 33         this.list=list;
 34     }
 35     //需要加强的方法
 36     @Override
 37     public void close() throws SQLException {
 38         //添加到连接池中
 39         System.out.println("归还前的大小:"+list.size());
 40         list.addFirst(this);
 41         System.out.println("归还后的大小:"+list.size());
 42         System.out.println("已经归还!");
 43     }
 44 
 45     @Override
 46     public Statement createStatement() throws SQLException {
 47         // TODO Auto-generated method stub
 48         return conn.createStatement();
 49     }
 50 
 51     @Override
 52     public PreparedStatement prepareStatement(String sql) throws SQLException {
 53         // TODO Auto-generated method stub
 54         return conn.prepareStatement(sql);
 55     }
 56 
 57     @Override
 58     public <T> T unwrap(Class<T> iface) throws SQLException {
 59         // TODO Auto-generated method stub
 60         return null;
 61     }
 62 
 63     @Override
 64     public boolean isWrapperFor(Class<?> iface) throws SQLException {
 65         // TODO Auto-generated method stub
 66         return false;
 67     }
 68 
 69     @Override
 70     public CallableStatement prepareCall(String sql) throws SQLException {
 71         // TODO Auto-generated method stub
 72         return null;
 73     }
 74 
 75     @Override
 76     public String nativeSQL(String sql) throws SQLException {
 77         // TODO Auto-generated method stub
 78         return null;
 79     }
 80 
 81     @Override
 82     public void setAutoCommit(boolean autoCommit) throws SQLException {
 83         // TODO Auto-generated method stub
 84 
 85     }
 86 
 87     @Override
 88     public boolean getAutoCommit() throws SQLException {
 89         // TODO Auto-generated method stub
 90         return false;
 91     }
 92 
 93     @Override
 94     public void commit() throws SQLException {
 95         // TODO Auto-generated method stub
 96 
 97     }
 98 
 99     @Override
100     public void rollback() throws SQLException {
101         // TODO Auto-generated method stub
102 
103     }
104 
105     @Override
106     public boolean isClosed() throws SQLException {
107         // TODO Auto-generated method stub
108         return false;
109     }
110 
111     @Override
112     public DatabaseMetaData getMetaData() throws SQLException {
113         // TODO Auto-generated method stub
114         return null;
115     }
116 
117     @Override
118     public void setReadOnly(boolean readOnly) throws SQLException {
119         // TODO Auto-generated method stub
120 
121     }
122 
123     @Override
124     public boolean isReadOnly() throws SQLException {
125         // TODO Auto-generated method stub
126         return false;
127     }
128 
129     @Override
130     public void setCatalog(String catalog) throws SQLException {
131         // TODO Auto-generated method stub
132 
133     }
134 
135     @Override
136     public String getCatalog() throws SQLException {
137         // TODO Auto-generated method stub
138         return null;
139     }
140 
141     @Override
142     public void setTransactionIsolation(int level) throws SQLException {
143         // TODO Auto-generated method stub
144 
145     }
146 
147     @Override
148     public int getTransactionIsolation() throws SQLException {
149         // TODO Auto-generated method stub
150         return 0;
151     }
152 
153     @Override
154     public SQLWarning getWarnings() throws SQLException {
155         // TODO Auto-generated method stub
156         return null;
157     }
158 
159     @Override
160     public void clearWarnings() throws SQLException {
161         // TODO Auto-generated method stub
162 
163     }
164 
165     @Override
166     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
167         // TODO Auto-generated method stub
168         return null;
169     }
170 
171     @Override
172     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
173             throws SQLException {
174         // TODO Auto-generated method stub
175         return null;
176     }
177 
178     @Override
179     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
180         // TODO Auto-generated method stub
181         return null;
182     }
183 
184     @Override
185     public Map<String, Class<?>> getTypeMap() throws SQLException {
186         // TODO Auto-generated method stub
187         return null;
188     }
189 
190     @Override
191     public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
192         // TODO Auto-generated method stub
193 
194     }
195 
196     @Override
197     public void setHoldability(int holdability) throws SQLException {
198         // TODO Auto-generated method stub
199 
200     }
201 
202     @Override
203     public int getHoldability() throws SQLException {
204         // TODO Auto-generated method stub
205         return 0;
206     }
207 
208     @Override
209     public Savepoint setSavepoint() throws SQLException {
210         // TODO Auto-generated method stub
211         return null;
212     }
213 
214     @Override
215     public Savepoint setSavepoint(String name) throws SQLException {
216         // TODO Auto-generated method stub
217         return null;
218     }
219 
220     @Override
221     public void rollback(Savepoint savepoint) throws SQLException {
222         // TODO Auto-generated method stub
223 
224     }
225 
226     @Override
227     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
228         // TODO Auto-generated method stub
229 
230     }
231 
232     @Override
233     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
234             throws SQLException {
235         // TODO Auto-generated method stub
236         return null;
237     }
238 
239     @Override
240     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
241             int resultSetHoldability) throws SQLException {
242         // TODO Auto-generated method stub
243         return null;
244     }
245 
246     @Override
247     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
248             int resultSetHoldability) throws SQLException {
249         // TODO Auto-generated method stub
250         return null;
251     }
252 
253     @Override
254     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
255         // TODO Auto-generated method stub
256         return null;
257     }
258 
259     @Override
260     public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
261         // TODO Auto-generated method stub
262         return null;
263     }
264 
265     @Override
266     public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
267         // TODO Auto-generated method stub
268         return null;
269     }
270 
271     @Override
272     public Clob createClob() throws SQLException {
273         // TODO Auto-generated method stub
274         return null;
275     }
276 
277     @Override
278     public Blob createBlob() throws SQLException {
279         // TODO Auto-generated method stub
280         return null;
281     }
282 
283     @Override
284     public NClob createNClob() throws SQLException {
285         // TODO Auto-generated method stub
286         return null;
287     }
288 
289     @Override
290     public SQLXML createSQLXML() throws SQLException {
291         // TODO Auto-generated method stub
292         return null;
293     }
294 
295     @Override
296     public boolean isValid(int timeout) throws SQLException {
297         // TODO Auto-generated method stub
298         return false;
299     }
300 
301     @Override
302     public void setClientInfo(String name, String value) throws SQLClientInfoException {
303         // TODO Auto-generated method stub
304 
305     }
306 
307     @Override
308     public void setClientInfo(Properties properties) throws SQLClientInfoException {
309         // TODO Auto-generated method stub
310 
311     }
312 
313     @Override
314     public String getClientInfo(String name) throws SQLException {
315         // TODO Auto-generated method stub
316         return null;
317     }
318 
319     @Override
320     public Properties getClientInfo() throws SQLException {
321         // TODO Auto-generated method stub
322         return null;
323     }
324 
325     @Override
326     public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
327         // TODO Auto-generated method stub
328         return null;
329     }
330 
331     @Override
332     public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
333         // TODO Auto-generated method stub
334         return null;
335     }
336 
337     @Override
338     public void setSchema(String schema) throws SQLException {
339         // TODO Auto-generated method stub
340 
341     }
342 
343     @Override
344     public String getSchema() throws SQLException {
345         // TODO Auto-generated method stub
346         return null;
347     }
348 
349     @Override
350     public void abort(Executor executor) throws SQLException {
351         // TODO Auto-generated method stub
352 
353     }
354 
355     @Override
356     public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
357         // TODO Auto-generated method stub
358 
359     }
360 
361     @Override
362     public int getNetworkTimeout() throws SQLException {
363         // TODO Auto-generated method stub
364         return 0;
365     }
366 
367 }
View Code

测试类

技术分享
 1 package cn.jdbc_myConn;
 2 
 3 import java.sql.Connection;
 4 import java.sql.SQLException;
 5 
 6 public class Test {
 7     public static void main(String[] args) throws SQLException {
 8     
 9         //获取连接
10         Connection conn= MyDataSource.getConnection();
11         System.out.println(conn);
12         conn.close();
13     }
14 }
View Code

 
















以上是关于自定义连接池(装饰者模式)的主要内容,如果未能解决你的问题,请参考以下文章

设计模式之装饰者模式

day07 c3p0连接池

JDBC(连接池) -- 02(II)

jdbc_dbcp连接池和装饰者模式概述

设计模式-装饰者模式

装饰者模式