サービスプロバイダーは、達成するためのJDBCフレームワーク

1.はじめにフレームワーク

サービスプロバイダフレームワークは、このようなAシステムを指します。より多くのサービスプロバイダがサービスを実装するために、システムは、サービスプロバイダの顧客サービス側の複数の実装を提供し、それらをもたらすことは複数の実装から分離しました。
サービスフレームワークは、4つのコンポーネントがあります。

  • サービスポート:のプロバイダ実装
  • プロバイダ登録API:システムは、登録を達成するために使用され、その結果、顧客サービスのエンドアクセスそれら
  • サービスアクセスAPI:サービスへのインスタンスにアクセスするためのクライアント
  • サービスプロバイダインタフェース(オプション):そのサービスの実装のインスタンスを作成するための責任

2.JDBC実装

JDBCデータベース接続は、2つの段階で重要であるドライバをロードすることで、接続して取得

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.接続を取得、登録ドライブを見つけ、実装を駆動するための接続メソッドを呼び出します

 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を話すために、接続は、サービス・インターフェース、にDriverManager.registerDriverプロバイダ登録API、たDriverManager.getConnectionサービスアクセスAPI、ドライバーがサービスプロバイダインタフェースです。

公開された107元の記事 ウォン称賛69 ビュー310 000 +

おすすめ

転載: blog.csdn.net/Tracycater/article/details/104597222