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是服務提供者接口。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章