1. 驱动加载,通过SPI机制,不需要Class.forName()加载驱动。
  2. 可以加载多个驱动
  3. 循环驱动,尝试连接,连接成功就返回。

    驱动加载

    DriverManager 在静态代码块中加载驱动,类路径下有多个驱动都会被加载。 ```java package java.sql

public class DriverManager { static { loadInitialDrivers(); println(“JDBC DriverManager initialized”); }

private static void loadInitialDrivers() { // 加载所有配置在/META-INF/services/java.sql.Driver的驱动实现类 ServiceLoader loadedDrivers = ServiceLoader.load(Driver.class); Iterator driversIterator = loadedDrivers.iterator(); while(driversIterator.hasNext()) { //加载并实例化 driversIterator.next(); } }

  1. MySQL 驱动类的静态代码块中注册驱动。
  2. ```java
  3. package com.mysql.cj.jdbc;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. public class Driver extends NonRegisteringDriver implements java.sql.Driver {
  7. public Driver() throws SQLException {
  8. }
  9. static {
  10. try {
  11. DriverManager.registerDriver(new Driver());
  12. } catch (SQLException var1) {
  13. throw new RuntimeException("Can't register driver!");
  14. }
  15. }
  16. }

Oracle驱动类的静态代码块中注册驱动。

  1. package oracle.jdbc.driver;
  2. @DefaultLogger("oracle.jdbc")
  3. @Supports({Feature.CONNECT})
  4. public class OracleDriver implements Driver, Monitor {
  5. static {
  6. if (defaultDriver == null) {
  7. defaultDriver = new oracle.jdbc.OracleDriver();
  8. DriverManager.registerDriver(defaultDriver, OracleDriver::deregister);
  9. }
  10. }
  11. }

总结:DriverManager 类加载 —> loadInitialDrivers —> ServiceLoader.load(Driver.class) —> next() —> 具体的驱动类加载 —> DriverManager.registerDriver.

获取连接

DriverManager.getConnection是尝试获取连接,本质还是调用Driver.getConnection. 如果直接用Driver.getConnection获取连接就可以确定的获取连接。

  1. java.sql.DriverManager
  2. private static Connection getConnection(
  3. String url, java.util.Properties info, Class<?> caller) throws SQLException {
  4. ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
  5. synchronized(DriverManager.class) {
  6. // synchronize loading of the correct classloader.
  7. if (callerCL == null) {
  8. callerCL = Thread.currentThread().getContextClassLoader();
  9. }
  10. }
  11. if(url == null) {
  12. throw new SQLException("The url cannot be null", "08001");
  13. }
  14. println("DriverManager.getConnection(\"" + url + "\")");
  15. // Walk through the loaded registeredDrivers attempting to make a connection.
  16. // Remember the first exception that gets raised so we can reraise it.
  17. SQLException reason = null;
  18. //循环尝试获取连接,获取就返回
  19. for(DriverInfo aDriver : registeredDrivers) {
  20. // If the caller does not have permission to load the driver then skip it.
  21. if(isDriverAllowed(aDriver.driver, callerCL)) {
  22. try {
  23. println(" trying " + aDriver.driver.getClass().getName());
  24. Connection con = aDriver.driver.connect(url, info);
  25. if (con != null) {
  26. // Success!
  27. println("getConnection returning "+aDriver.driver.getClass().getName());
  28. return (con);
  29. }
  30. } catch (SQLException ex) {
  31. if (reason == null) {
  32. reason = ex;
  33. }
  34. }
  35. } else {
  36. println(" skipping: " + aDriver.getClass().getName());
  37. }
  38. }
  39. // if we got here nobody could connect.
  40. if (reason != null) {
  41. println("getConnection failed: " + reason);
  42. throw reason;
  43. }
  44. println("getConnection: no suitable driver found for "+ url);
  45. throw new SQLException("No suitable driver found for "+ url, "08001");
  46. }