JDBC学习之路(十)编写一个基本的连接池来实现连接的复用

               

众所周知,在JDBC创建或者使用的过程中,第一步创建连接是最复杂的也是最耗费时间的,因为它是一个网络操

作,需要不断的去尝试连接地址,所以建立好一个连接以后仅仅发送少量的sql语句就将其关闭是很不划算的,所以这

就诞生了连接池这个概念。所谓的连接池就是将多个创建好的连接放入到一个容器中,当使用的时候就从这个容器中

取出来连接,使用完成之后再把这个连接放回到容器中去,这样就完成了连接的复用,这样的想法是一个简单的连接池

的实现,当然了呃,真正的连接池要考虑的因素还很多,我们这里只是说说最简单的和想法,下面来说明一下这个最简

单的连接池的开发。

package com.bird.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.LinkedList;/** * 一个简单的连接池的实现 * @author Bird * */public class MyDataSource private static String url = "jdbc:mysql://localhost:3306/test"private static String username = "root"private static String password = "mysql";  private static int initCount = 5private static int maxCount = 10private static int currentCount = 0private LinkedList<Connection> connectionsPool = new LinkedList<Connection>(); public MyDataSource() {  for (int i = 0; i < initCount; i++) {   try {    this.connectionsPool.add(creatConnection());    currentCount++;   } catch (SQLException e) {    throw new ExceptionInInitializerError(e);   }  } }   public Connection getConnection() throws SQLException{//从连接池中拿走一个连接  synchronized (connectionsPool) {   if(connectionsPool.size() > 0)   return connectionsPool.remove();      if(connectionsPool.size() < maxCount){    currentCount++;    return this.creatConnection();   }  }  throw new SQLException("超过最大连接");  }   public void free(Connection conn){//释放连接,把连接放到连接池中  this.connectionsPool.add(conn); }  private Connection creatConnection() throws SQLException {  return DriverManager.getConnection(url, username, password); }}


 

然后对于这个使用也是很简单,就是把这些连接更改在工具类中就算可以了,这体现了工具类的重要

package com.bird.jdbc;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * JDBC工具类 * @author bird * */public final class Temple private static MyDataSource dataSource;  private Temple() { } static {  try {   Class.forName("com.mysql.jdbc.Driver");   dataSource = new MyDataSource();  } catch (ClassNotFoundException e) {   throw new ExceptionInInitializerError(e);  } } public static Connection getConnection() throws SQLException {  return dataSource.getConnection();   } public static void free(Connection con, Statement st, ResultSet rs) {  try {   if (rs != null)    rs.close();  } catch (SQLException e) {   e.printStackTrace();  } finally {   try {    if (st != null)     st.close();   } catch (SQLException e) {    e.printStackTrace();   } finally {    if (con != null)     dataSource.free(con);   }  } } }

就是在需要关闭连接的时候不把他关闭,而是仅仅把它放入到容器中,就这样而已。我们测试了一下,连续取出10次

连接,是可以发现有复用的连接的

com.mysql.jdbc.JDBC4Connection@a39137com.mysql.jdbc.JDBC4Connection@92e78ccom.mysql.jdbc.JDBC4Connection@9fbe93com.mysql.jdbc.JDBC4Connection@198dfafcom.mysql.jdbc.JDBC4Connection@1858610com.mysql.jdbc.JDBC4Connection@a39137com.mysql.jdbc.JDBC4Connection@92e78ccom.mysql.jdbc.JDBC4Connection@9fbe93com.mysql.jdbc.JDBC4Connection@198dfafcom.mysql.jdbc.JDBC4Connection@1858610


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43679366/article/details/87268395