在使用druid数据库连接池时driverClassName数据库驱动程序类可以配置,也可以不配置程序都可以正常运行,缺省配置是如何正确找到驱动程序呢?
com.alibaba.druid.pool.DruidDataSource#init数据库连接池初始化方法中:
public void init() throws SQLException {
...
initFromSPIServiceLoader();
resolveDriver();
...
}
com.alibaba.druid.pool.DruidDataSource#resolveDriver解析驱动程序类:
protected void resolveDriver() throws SQLException {
if (this.driver == null) {
if (this.driverClass == null || this.driverClass.isEmpty()) {
this.driverClass = JdbcUtils.getDriverClassName(this.jdbcUrl);
}
if (MockDriver.class.getName().equals(driverClass)) {
driver = MockDriver.instance;
} else if ("com.alibaba.druid.support.clickhouse.BalancedClickhouseDriver".equals(driverClass)) {
Properties info = new Properties();
info.put("user", username);
info.put("password", password);
info.putAll(connectProperties);
driver = new BalancedClickhouseDriver(jdbcUrl, info);
} else {
if (jdbcUrl == null && (driverClass == null || driverClass.length() == 0)) {
throw new SQLException("url not set");
}
driver = JdbcUtils.createDriver(driverClassLoader, driverClass);
}
} else {
if (this.driverClass == null) {
this.driverClass = driver.getClass().getName();
}
}
}
此处创建驱动程序类之后会自动的注册java.sql.DriverManager#registerDriver(java.sql.Driver, java.sql.DriverAction)驱动类;
com.alibaba.druid.util.JdbcUtils#getDriverClassName根据url获取驱动程序:
public static String getDriverClassName(String rawUrl) throws SQLException {
if (rawUrl == null) {
return null;
}
if (rawUrl.startsWith("jdbc:derby:")) {
return "org.apache.derby.jdbc.EmbeddedDriver";
} else if (rawUrl.startsWith("jdbc:mysql:")) {
if (mysql_driver_version_6 == null) {
mysql_driver_version_6 = Utils.loadClass("com.mysql.cj.jdbc.Driver") != null;
}
if (mysql_driver_version_6) {
return MYSQL_DRIVER_6;
} else {
return MYSQL_DRIVER;
}
} else if (rawUrl.startsWith("jdbc:log4jdbc:")) {
return LOG4JDBC_DRIVER;
} else if (rawUrl.startsWith("jdbc:mariadb:")) {
return MARIADB_DRIVER;
} else if (rawUrl.startsWith("jdbc:oracle:")
|| rawUrl.startsWith("JDBC:oracle:")) {
return ORACLE_DRIVER;
} else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) {
return ALI_ORACLE_DRIVER;
} else if (rawUrl.startsWith("jdbc:oceanbase:")) {
return OCEANBASE_DRIVER;
} else if (rawUrl.startsWith("jdbc:microsoft:")) {
return "com.microsoft.jdbc.sqlserver.SQLServerDriver";
} else if (rawUrl.startsWith("jdbc:sqlserver:")) {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
} else if (rawUrl.startsWith("jdbc:sybase:Tds:")) {
return "com.sybase.jdbc2.jdbc.SybDriver";
} else if (rawUrl.startsWith("jdbc:jtds:")) {
return "net.sourceforge.jtds.jdbc.Driver";
} else if (rawUrl.startsWith("jdbc:fake:") || rawUrl.startsWith("jdbc:mock:")) {
return "com.alibaba.druid.mock.MockDriver";
} else if (rawUrl.startsWith("jdbc:postgresql:")) {
return POSTGRESQL_DRIVER;
} else if (rawUrl.startsWith("jdbc:edb:")) {
return ENTERPRISEDB_DRIVER;
} else if (rawUrl.startsWith("jdbc:odps:")) {
return ODPS_DRIVER;
} else if (rawUrl.startsWith("jdbc:hsqldb:")) {
return "org.hsqldb.jdbcDriver";
} else if (rawUrl.startsWith("jdbc:db2:")) {
String prefix = "jdbc:db2:";
if (rawUrl.startsWith(prefix + "//")) {
return DB2_DRIVER;
} else {
String suffix = rawUrl.substring(prefix.length());
if (suffix.indexOf(':') > 0) {
return DB2_DRIVER3;
} else {
return DB2_DRIVER2;
}
}
} else if (rawUrl.startsWith("jdbc:sqlite:")) {
return SQLITE_DRIVER;
} else if (rawUrl.startsWith("jdbc:ingres:")) {
return "com.ingres.jdbc.IngresDriver";
} else if (rawUrl.startsWith("jdbc:h2:")) {
return H2_DRIVER;
} else if (rawUrl.startsWith("jdbc:mckoi:")) {
return "com.mckoi.JDBCDriver";
} else if (rawUrl.startsWith("jdbc:cloudscape:")) {
return "COM.cloudscape.core.JDBCDriver";
} else if (rawUrl.startsWith("jdbc:informix-sqli:")) {
return "com.informix.jdbc.IfxDriver";
} else if (rawUrl.startsWith("jdbc:timesten:")) {
return "com.timesten.jdbc.TimesTenDriver";
} else if (rawUrl.startsWith("jdbc:as400:")) {
return "com.ibm.as400.access.AS400JDBCDriver";
} else if (rawUrl.startsWith("jdbc:sapdb:")) {
return "com.sap.dbtech.jdbc.DriverSapDB";
} else if (rawUrl.startsWith("jdbc:JSQLConnect:")) {
return "com.jnetdirect.jsql.JSQLDriver";
} else if (rawUrl.startsWith("jdbc:JTurbo:")) {
return "com.newatlanta.jturbo.driver.Driver";
} else if (rawUrl.startsWith("jdbc:firebirdsql:")) {
return "org.firebirdsql.jdbc.FBDriver";
} else if (rawUrl.startsWith("jdbc:interbase:")) {
return "interbase.interclient.Driver";
} else if (rawUrl.startsWith("jdbc:pointbase:")) {
return "com.pointbase.jdbc.jdbcUniversalDriver";
} else if (rawUrl.startsWith("jdbc:edbc:")) {
return "ca.edbc.jdbc.EdbcDriver";
} else if (rawUrl.startsWith("jdbc:mimer:multi1:")) {
return "com.mimer.jdbc.Driver";
} else if (rawUrl.startsWith("jdbc:dm:")) {
return JdbcConstants.DM_DRIVER;
} else if (rawUrl.startsWith("jdbc:kingbase:")) {
return JdbcConstants.KINGBASE_DRIVER;
} else if (rawUrl.startsWith("jdbc:kingbase8:")) {
return JdbcConstants.KINGBASE8_DRIVER;
} else if (rawUrl.startsWith("jdbc:gbase:")) {
return JdbcConstants.GBASE_DRIVER;
} else if (rawUrl.startsWith("jdbc:xugu:")) {
return JdbcConstants.XUGU_DRIVER;
} else if (rawUrl.startsWith("jdbc:hive:")) {
return JdbcConstants.HIVE_DRIVER;
} else if (rawUrl.startsWith("jdbc:hive2:")) {
return JdbcConstants.HIVE_DRIVER;
} else if (rawUrl.startsWith("jdbc:phoenix:thin:")) {
return "org.apache.phoenix.queryserver.client.Driver";
} else if (rawUrl.startsWith("jdbc:phoenix://")) {
return JdbcConstants.PHOENIX_DRIVER;
} else if (rawUrl.startsWith("jdbc:kylin:")) {
return JdbcConstants.KYLIN_DRIVER;
} else if (rawUrl.startsWith("jdbc:elastic:")) {
return JdbcConstants.ELASTIC_SEARCH_DRIVER;
} else if (rawUrl.startsWith("jdbc:clickhouse:")) {
return JdbcConstants.CLICKHOUSE_DRIVER;
} else if(rawUrl.startsWith("jdbc:presto:")) {
return JdbcConstants.PRESTO_DRIVER;
} else if(rawUrl.startsWith("jdbc:trino:")) {
return JdbcConstants.TRINO_DRIVER;
} else if (rawUrl.startsWith("jdbc:inspur:")) {
return JdbcConstants.KDB_DRIVER;
} else if (rawUrl.startsWith("jdbc:polardb")) {
return JdbcConstants.POLARDB_DRIVER;
} else if (rawUrl.startsWith("jdbc:highgo:")) {
return "com.highgo.jdbc.Driver";
} else {
throw new SQLException("unknown jdbc driver : " + rawUrl);
}
}
GitHub地址:https://github.com/mingyang66/spring-parent
|