连接池3:代理模式重写close方法

怼烎@ 2022-08-09 01:54 155阅读 0赞

这种代码在实际中很少会使用,因为会有现成的框架来使用。

1,新的数据源

  1. package cn.itcast.jdbc.dataSource;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.util.LinkedList;
  6. public class MyDataSource2 {
  7. private static String url = "jdbc:mysql://localhost:3306/jdbc";
  8. private static String user = "root";
  9. private static String password = "123456";
  10. private static int initCount = 5;
  11. private static int maxCount = 10;
  12. private int currentCount = 0;
  13. public static void main(String[] args) {
  14. }
  15. LinkedList<Connection> connectionPool = new LinkedList<Connection>();
  16. public MyDataSource2() {
  17. try {
  18. for (int i = 0; i < initCount; i++) {
  19. this.connectionPool.addLast(this.createConnection());
  20. this.currentCount++;
  21. }
  22. } catch (SQLException e) {
  23. throw new ExceptionInInitializerError();
  24. }
  25. }
  26. public Connection getConnection() throws SQLException {
  27. synchronized (connectionPool) {
  28. if (this.connectionPool.size() > 0)
  29. return this.connectionPool.removeFirst();
  30. if (this.currentCount < maxCount) {
  31. this.currentCount++;
  32. return this.createConnection();
  33. }
  34. throw new SQLException("已经没有可用连接");
  35. }
  36. }
  37. public void free(Connection conn) {
  38. this.connectionPool.addLast(conn);
  39. }
  40. private Connection createConnection() throws SQLException {
  41. Connection realConn = DriverManager.getConnection(url, user, password);
  42. MyConnection myConnection = new MyConnection(realConn, this);
  43. return myConnection;
  44. }
  45. }

2,修改close方法
实现Connection接口

  1. package cn.itcast.jdbc.dataSource;
  2. import java.sql.Array;
  3. import java.sql.Blob;
  4. import java.sql.CallableStatement;
  5. import java.sql.Clob;
  6. import java.sql.Connection;
  7. import java.sql.DatabaseMetaData;
  8. import java.sql.NClob;
  9. import java.sql.PreparedStatement;
  10. import java.sql.SQLClientInfoException;
  11. import java.sql.SQLException;
  12. import java.sql.SQLWarning;
  13. import java.sql.SQLXML;
  14. import java.sql.Savepoint;
  15. import java.sql.Statement;
  16. import java.sql.Struct;
  17. import java.util.Map;
  18. import java.util.Properties;
  19. import java.util.concurrent.Executor;
  20. public class MyConnection implements Connection {
  21. private Connection realConnection;
  22. private MyDataSource2 dataSource;
  23. MyConnection(java.sql.Connection connection, MyDataSource2 dataSource) {
  24. realConnection = connection;
  25. this.dataSource = dataSource;
  26. }
  27. @Override
  28. public void close() throws SQLException { //修改close方法,放回连接池而不是关闭连接
  29. this.dataSource.connectionPool.addLast(this);
  30. }
  31. @Override
  32. public <T> T unwrap(Class<T> iface) throws SQLException {
  33. return this.realConnection.unwrap(iface); // 后面的做法相同,都交给realConnection去做
  34. }
  35. @Override
  36. public boolean isWrapperFor(Class<?> iface) throws SQLException {
  37. // TODO Auto-generated method stub
  38. return false;
  39. }
  40. @Override
  41. public Statement createStatement() throws SQLException {
  42. // TODO Auto-generated method stub
  43. return null;
  44. }
  45. @Override
  46. public PreparedStatement prepareStatement(String sql) throws SQLException {
  47. // TODO Auto-generated method stub
  48. return null;
  49. }
  50. @Override
  51. public CallableStatement prepareCall(String sql) throws SQLException {
  52. // TODO Auto-generated method stub
  53. return null;
  54. }
  55. @Override
  56. public String nativeSQL(String sql) throws SQLException {
  57. // TODO Auto-generated method stub
  58. return null;
  59. }
  60. @Override
  61. public void setAutoCommit(boolean autoCommit) throws SQLException {
  62. // TODO Auto-generated method stub
  63. }
  64. @Override
  65. public boolean getAutoCommit() throws SQLException {
  66. // TODO Auto-generated method stub
  67. return false;
  68. }
  69. @Override
  70. public void commit() throws SQLException {
  71. // TODO Auto-generated method stub
  72. }
  73. @Override
  74. public void rollback() throws SQLException {
  75. // TODO Auto-generated method stub
  76. }
  77. @Override
  78. public boolean isClosed() throws SQLException {
  79. // TODO Auto-generated method stub
  80. return false;
  81. }
  82. @Override
  83. public DatabaseMetaData getMetaData() throws SQLException {
  84. // TODO Auto-generated method stub
  85. return null;
  86. }
  87. @Override
  88. public void setReadOnly(boolean readOnly) throws SQLException {
  89. // TODO Auto-generated method stub
  90. }
  91. @Override
  92. public boolean isReadOnly() throws SQLException {
  93. // TODO Auto-generated method stub
  94. return false;
  95. }
  96. @Override
  97. public void setCatalog(String catalog) throws SQLException {
  98. // TODO Auto-generated method stub
  99. }
  100. @Override
  101. public String getCatalog() throws SQLException {
  102. // TODO Auto-generated method stub
  103. return null;
  104. }
  105. @Override
  106. public void setTransactionIsolation(int level) throws SQLException {
  107. // TODO Auto-generated method stub
  108. }
  109. @Override
  110. public int getTransactionIsolation() throws SQLException {
  111. // TODO Auto-generated method stub
  112. return 0;
  113. }
  114. @Override
  115. public SQLWarning getWarnings() throws SQLException {
  116. // TODO Auto-generated method stub
  117. return null;
  118. }
  119. @Override
  120. public void clearWarnings() throws SQLException {
  121. // TODO Auto-generated method stub
  122. }
  123. @Override
  124. public Statement createStatement(int resultSetType, int resultSetConcurrency)
  125. throws SQLException {
  126. // TODO Auto-generated method stub
  127. return null;
  128. }
  129. @Override
  130. public PreparedStatement prepareStatement(String sql, int resultSetType,
  131. int resultSetConcurrency) throws SQLException {
  132. // TODO Auto-generated method stub
  133. return null;
  134. }
  135. @Override
  136. public CallableStatement prepareCall(String sql, int resultSetType,
  137. int resultSetConcurrency) throws SQLException {
  138. // TODO Auto-generated method stub
  139. return null;
  140. }
  141. @Override
  142. public Map<String, Class<?>> getTypeMap() throws SQLException {
  143. // TODO Auto-generated method stub
  144. return null;
  145. }
  146. @Override
  147. public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
  148. // TODO Auto-generated method stub
  149. }
  150. @Override
  151. public void setHoldability(int holdability) throws SQLException {
  152. // TODO Auto-generated method stub
  153. }
  154. @Override
  155. public int getHoldability() throws SQLException {
  156. // TODO Auto-generated method stub
  157. return 0;
  158. }
  159. @Override
  160. public Savepoint setSavepoint() throws SQLException {
  161. // TODO Auto-generated method stub
  162. return null;
  163. }
  164. @Override
  165. public Savepoint setSavepoint(String name) throws SQLException {
  166. // TODO Auto-generated method stub
  167. return null;
  168. }
  169. @Override
  170. public void rollback(Savepoint savepoint) throws SQLException {
  171. // TODO Auto-generated method stub
  172. }
  173. @Override
  174. public void releaseSavepoint(Savepoint savepoint) throws SQLException {
  175. // TODO Auto-generated method stub
  176. }
  177. @Override
  178. public Statement createStatement(int resultSetType,
  179. int resultSetConcurrency, int resultSetHoldability)
  180. throws SQLException {
  181. // TODO Auto-generated method stub
  182. return null;
  183. }
  184. @Override
  185. public PreparedStatement prepareStatement(String sql, int resultSetType,
  186. int resultSetConcurrency, int resultSetHoldability)
  187. throws SQLException {
  188. // TODO Auto-generated method stub
  189. return null;
  190. }
  191. @Override
  192. public CallableStatement prepareCall(String sql, int resultSetType,
  193. int resultSetConcurrency, int resultSetHoldability)
  194. throws SQLException {
  195. // TODO Auto-generated method stub
  196. return null;
  197. }
  198. @Override
  199. public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
  200. throws SQLException {
  201. // TODO Auto-generated method stub
  202. return null;
  203. }
  204. @Override
  205. public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
  206. throws SQLException {
  207. // TODO Auto-generated method stub
  208. return null;
  209. }
  210. @Override
  211. public PreparedStatement prepareStatement(String sql, String[] columnNames)
  212. throws SQLException {
  213. // TODO Auto-generated method stub
  214. return null;
  215. }
  216. @Override
  217. public Clob createClob() throws SQLException {
  218. // TODO Auto-generated method stub
  219. return null;
  220. }
  221. @Override
  222. public Blob createBlob() throws SQLException {
  223. // TODO Auto-generated method stub
  224. return null;
  225. }
  226. @Override
  227. public NClob createNClob() throws SQLException {
  228. // TODO Auto-generated method stub
  229. return null;
  230. }
  231. @Override
  232. public SQLXML createSQLXML() throws SQLException {
  233. // TODO Auto-generated method stub
  234. return null;
  235. }
  236. @Override
  237. public boolean isValid(int timeout) throws SQLException {
  238. // TODO Auto-generated method stub
  239. return false;
  240. }
  241. @Override
  242. public void setClientInfo(String name, String value)
  243. throws SQLClientInfoException {
  244. // TODO Auto-generated method stub
  245. }
  246. @Override
  247. public void setClientInfo(Properties properties)
  248. throws SQLClientInfoException {
  249. // TODO Auto-generated method stub
  250. }
  251. @Override
  252. public String getClientInfo(String name) throws SQLException {
  253. // TODO Auto-generated method stub
  254. return null;
  255. }
  256. @Override
  257. public Properties getClientInfo() throws SQLException {
  258. // TODO Auto-generated method stub
  259. return null;
  260. }
  261. @Override
  262. public Array createArrayOf(String typeName, Object[] elements)
  263. throws SQLException {
  264. // TODO Auto-generated method stub
  265. return null;
  266. }
  267. @Override
  268. public Struct createStruct(String typeName, Object[] attributes)
  269. throws SQLException {
  270. // TODO Auto-generated method stub
  271. return null;
  272. }
  273. @Override
  274. public void setSchema(String schema) throws SQLException {
  275. // TODO Auto-generated method stub
  276. }
  277. @Override
  278. public String getSchema() throws SQLException {
  279. // TODO Auto-generated method stub
  280. return null;
  281. }
  282. @Override
  283. public void abort(Executor executor) throws SQLException {
  284. // TODO Auto-generated method stub
  285. }
  286. @Override
  287. public void setNetworkTimeout(Executor executor, int milliseconds)
  288. throws SQLException {
  289. // TODO Auto-generated method stub
  290. }
  291. @Override
  292. public int getNetworkTimeout() throws SQLException {
  293. // TODO Auto-generated method stub
  294. return 0;
  295. }
  296. }

3,修改工具类

  1. package cn.itcast.jdbc;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import cn.itcast.jdbc.dataSource.MyDataSource2;
  7. public final class JdbcUtils {
  8. private static MyDataSource2 myDataSource = null;
  9. private JdbcUtils() {
  10. }
  11. static {
  12. try {
  13. Class.forName("com.mysql.jdbc.Driver");
  14. myDataSource = new MyDataSource2();
  15. } catch (Exception e) {
  16. throw new ExceptionInInitializerError(e);
  17. }
  18. }
  19. public static Connection getConnection() throws SQLException {
  20. // return DriverManager.getConnection(url, user, password);
  21. return myDataSource.getConnection(); // 取连接
  22. }
  23. public static void free(ResultSet rs, Statement st, Connection conn) {
  24. try {
  25. if (rs != null)
  26. rs.close();
  27. } catch (SQLException e) {
  28. e.printStackTrace();
  29. } finally {
  30. try {
  31. if (st != null)
  32. st.close();//现在close方法就是将连接放回连接池,而不是关闭连接
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. } finally {
  36. try {
  37. if (conn != null)
  38. conn.close();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. }
  45. }

发表评论

表情:
评论列表 (有 0 条评论,155人围观)

还没有评论,来说两句吧...

相关阅读