JDBC实现的服务提供者框架

1.框架介绍

服务提供者框架是指这样一个系统:多个服务提供者实现一个服务,系统为服务提供者的客服端提供多个实现,并把他们从多个实现中解耦出来。
服务框架中有四个组件:

  • 服务接口:用来提供者实现的
  • 提供者注册API:系统用来注册实现,让客服端访问它们
  • 服务访问API:客户端用来获取服务的实例
  • 服务提供者接口(可选):负责创建其服务实现的实例

2.JDBC的实现

JDBC连接数据库重要的两步就是加载驱动和获取连接

1.加载驱动,通过执行驱动中静态代码块,进行注册到DiverManager中。

 #加载驱动
 Class.forName("com.mysql.cj.jdbc.Driver");

private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<>();
#加载mysql驱动的时候,完成注册
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    public Driver() throws SQLException {
    }

    static {
        try {
        #注册服务实现
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
}

 public static synchronized void registerDriver(java.sql.Driver driver)
      throws SQLException {

      registerDriver(driver, null);
  }
 public static synchronized void registerDriver(java.sql.Driver driver,
          DriverAction da)
      throws SQLException {
      // 如果不存在,注册驱动到集合中
      if(driver != null) {
          registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
      } else {
          throw new NullPointerException();
      }
      println("registerDriver: " + driver);
  }

2.获取连接,找到注册的驱动,调用驱动实现的connect方法

 Connection conn=DriverManager.getConnection(url, user, password);
 
 public static Connection getConnection(String url,
        String user, String password) throws SQLException {
        java.util.Properties info = new java.util.Properties();

        if (user != null) {
            info.put("user", user);
        }
        if (password != null) {
            info.put("password", password);
        }

        return (getConnection(url, info, Reflection.getCallerClass()));
    }
    
private static Connection getConnection(
        String url, java.util.Properties info, Class<?> caller) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
        synchronized(DriverManager.class) {
            // synchronize loading of the correct classloader.
            if (callerCL == null) {
                callerCL = Thread.currentThread().getContextClassLoader();
            }
        }

        if(url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }

        println("DriverManager.getConnection(\"" + url + "\")");

        // Walk through the loaded registeredDrivers attempting to make a connection.
        // Remember the first exception that gets raised so we can reraise it.
        SQLException reason = null;

        for(DriverInfo aDriver : registeredDrivers) {
            // If the caller does not have permission to load the driver then
            // skip it.
            if(isDriverAllowed(aDriver.driver, callerCL)) {
                try {
                    println("    trying " + aDriver.driver.getClass().getName());
                    Connection con = aDriver.driver.connect(url, info);
                    if (con != null) {
                        // Success!
                        println("getConnection returning " + aDriver.driver.getClass().getName());
                        return (con);
                    }
                } catch (SQLException ex) {
                    if (reason == null) {
                        reason = ex;
                    }
                }

            } else {
                println("    skipping: " + aDriver.getClass().getName());
            }

        }

        // if we got here nobody could connect.
        if (reason != null)    {
            println("getConnection failed: " + reason);
            throw reason;
        }

        println("getConnection: no suitable driver found for "+ url);
        throw new SQLException("No suitable driver found for "+ url, "08001");
    }

对于JDBC来说,Connection是服务接口,DriverManager.registerDriver提供者注册API,DriverManager.getConnection是服务访问API,Driver是服务提供者接口。

JDBC实现的服务提供者框架JDBC实现的服务提供者框架 罗罗诺亚-小鱼 发布了107 篇原创文章 · 获赞 69 · 访问量 31万+ 私信 关注
上一篇:JDBCToolsV2


下一篇:JDBC——Statement执行SQL语句的对象