JDBC framework for service providers to achieve

1. Introduction framework

Service provider framework refers to such a system: more service providers to implement a service, the system provides multiple implementations of the service provider's customer service side, and bring them decoupled from multiple implementations.
Services framework has four components:

  • Service Port: The provider implementation of
  • Provider registration API: The system is used to achieve registration, so that the customer service end access them
  • Service Access API: Client for instance access to services
  • Service Provider Interface (optional): responsible for creating instances of its service implementation

2.JDBC implementation

JDBC database connection is important in two steps is to load drivers and get connected

1. Driver loading, by performing static driving block, registered in the 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. Get connected, find the registration drive, call the connect method for driving implementation

 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");
    }

For JDBC speaking, Connection is a service interface, DriverManager.registerDriver provider registration API, DriverManager.getConnection service access API, Driver is the service provider interface.

Published 107 original articles · won praise 69 · views 310 000 +

Guess you like

Origin blog.csdn.net/Tracycater/article/details/104597222