在druid 1.2.16以及之前版本中,可以允许dbType =null,不影响数据库连接使用
在druid 1.2.17以及之后版本中,已经不允许dbType =null
代码差异如下
1.2.16
if (connectTimeout > 0) {
if (isMySql) {
if (connectTimeoutStr == null) {
connectTimeoutStr = Integer.toString(connectTimeout);
}
physicalConnectProperties.put("connectTimeout", connectTimeoutStr);
} else if (isOracle) {
if (connectTimeoutStr == null) {
connectTimeoutStr = Integer.toString(connectTimeout);
}
physicalConnectProperties.put("oracle.net.CONNECT_TIMEOUT", connectTimeoutStr);
} else if (driver != null && "org.postgresql.Driver".equals(driver.getClass().getName())) {
physicalConnectProperties.put("loginTimeout", connectTimeout);
physicalConnectProperties.put("socketTimeout", socketTimeout);
}
}
1.2.17
if (connectTimeout > 0) {
if (isMySql) {
if (connectTimeoutStr == null) {
connectTimeoutStr = Integer.toString(connectTimeout);
}
physicalConnectProperties.put("connectTimeout", connectTimeoutStr);
} else if (isOracle) {
if (connectTimeoutStr == null) {
connectTimeoutStr = Integer.toString(connectTimeout);
}
physicalConnectProperties.put("oracle.net.CONNECT_TIMEOUT", connectTimeoutStr);
} else if (driver != null && POSTGRESQL_DRIVER.equals(driver.getClass().getName())) {
// see https://github.com/pgjdbc/pgjdbc/blob/2b90ad04696324d107b65b085df4b1db8f6c162d/README.md
physicalConnectProperties.put("loginTimeout", Long.toString(TimeUnit.MILLISECONDS.toSeconds(connectTimeout)));
physicalConnectProperties.put("connectTimeout", Long.toString(TimeUnit.MILLISECONDS.toSeconds(connectTimeout)));
if (socketTimeout > 0) {
physicalConnectProperties.put("socketTimeout", Long.toString(TimeUnit.MILLISECONDS.toSeconds(socketTimeout)));
}
} else if (dbTypeName.equals(DbType.sqlserver.name())) {
// see https://learn.microsoft.com/en-us/sql/connect/jdbc/setting-the-connection-properties?view=sql-server-ver16
physicalConnectProperties.put("loginTimeout", Long.toString(TimeUnit.MILLISECONDS.toSeconds(connectTimeout)));
if (socketTimeout > 0) {
// As SQLServer-jdbc-driver 6.1.2 can use this, see https://github.com/microsoft/mssql-jdbc/wiki/SocketTimeout
physicalConnectProperties.put("socketTimeout", Integer.toString(socketTimeout));
}
}
}
其中对dbTypeName做了强制校验,此处会出现空指针异常
在不改动源码的情况下使用druid,不能使用DruidDataSourceFactory工厂类去初始化数据源
可以自定义数据源去绕开dbTypeName
即自己去构造数据源参数
roperties pro = new Properties();
pro.load(test.class.getClassLoader().getResourceAsStream("druid.properties"));
DruidDataSource ds = new DruidDataSource();
ds.setUrl(pro.getProperty("url"));
ds.setUsername(pro.getProperty("username"));
ds.setPassword(pro.getProperty("password"));
ds.setDriverClassName(pro.getProperty("driverClassName"));
ds.setDbType(pro.getProperty("dbType"));
但此时不能使用JdbcUtils.getDbType 去拿到typename,因其不再内置的数据源中
public static DbType getDbTypeRaw(String rawUrl, String driverClassName) {
if (rawUrl == null) {
return null;
}
if (rawUrl.startsWith("jdbc:derby:") || rawUrl.startsWith("jdbc:log4jdbc:derby:")) {
return DbType.derby;
} else if (rawUrl.startsWith("jdbc:mysql:") || rawUrl.startsWith("jdbc:cobar:")
|| rawUrl.startsWith("jdbc:log4jdbc:mysql:")) {
return DbType.mysql;
} else if (rawUrl.startsWith("jdbc:mariadb:")) {
return DbType.mariadb;
} else if (rawUrl.startsWith("jdbc:tidb:")) {
return DbType.tidb;
} else if (rawUrl.startsWith("jdbc:oracle:") || rawUrl.startsWith("jdbc:log4jdbc:oracle:")) {
return DbType.oracle;
} else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) {
return DbType.ali_oracle;
} else if (rawUrl.startsWith("jdbc:oceanbase:oracle:")) {
return DbType.oceanbase_oracle;
} else if (rawUrl.startsWith("jdbc:oceanbase:")) {
return DbType.oceanbase;
} else if (rawUrl.startsWith("jdbc:microsoft:") || rawUrl.startsWith("jdbc:log4jdbc:microsoft:")) {
return DbType.sqlserver;
} else if (rawUrl.startsWith("jdbc:sqlserver:") || rawUrl.startsWith("jdbc:log4jdbc:sqlserver:")) {
return DbType.sqlserver;
} else if (rawUrl.startsWith("jdbc:sybase:Tds:") || rawUrl.startsWith("jdbc:log4jdbc:sybase:")) {
return DbType.sybase;
} else if (rawUrl.startsWith("jdbc:jtds:") || rawUrl.startsWith("jdbc:log4jdbc:jtds:")) {
return DbType.jtds;
} else if (rawUrl.startsWith("jdbc:fake:") || rawUrl.startsWith("jdbc:mock:")) {
return DbType.mock;
} else if (rawUrl.startsWith("jdbc:postgresql:") || rawUrl.startsWith("jdbc:log4jdbc:postgresql:")) {
return DbType.postgresql;
} else if (rawUrl.startsWith("jdbc:edb:")) {
return DbType.edb;
} else if (rawUrl.startsWith("jdbc:hsqldb:") || rawUrl.startsWith("jdbc:log4jdbc:hsqldb:")) {
return DbType.hsql;
} else if (rawUrl.startsWith("jdbc:odps:")) {
return DbType.odps;
} else if (rawUrl.startsWith("jdbc:db2:")) {
return DbType.db2;
} else if (rawUrl.startsWith("jdbc:sqlite:")) {
return DbType.sqlite;
} else if (rawUrl.startsWith("jdbc:ingres:")) {
return DbType.ingres;
} else if (rawUrl.startsWith("jdbc:h2:") || rawUrl.startsWith("jdbc:log4jdbc:h2:")) {
return DbType.h2;
} else if (rawUrl.startsWith("jdbc:mckoi:")) {
return DbType.mock;
} else if (rawUrl.startsWith("jdbc:cloudscape:")) {
return DbType.cloudscape;
} else if (rawUrl.startsWith("jdbc:informix-sqli:") || rawUrl.startsWith("jdbc:log4jdbc:informix-sqli:")) {
return DbType.informix;
} else if (rawUrl.startsWith("jdbc:timesten:")) {
return DbType.timesten;
} else if (rawUrl.startsWith("jdbc:as400:")) {
return DbType.as400;
} else if (rawUrl.startsWith("jdbc:sapdb:")) {
return DbType.sapdb;
} else if (rawUrl.startsWith("jdbc:JSQLConnect:")) {
return DbType.JSQLConnect;
} else if (rawUrl.startsWith("jdbc:JTurbo:")) {
return DbType.JTurbo;
} else if (rawUrl.startsWith("jdbc:firebirdsql:")) {
return DbType.firebirdsql;
} else if (rawUrl.startsWith("jdbc:interbase:")) {
return DbType.interbase;
} else if (rawUrl.startsWith("jdbc:pointbase:")) {
return DbType.pointbase;
} else if (rawUrl.startsWith("jdbc:edbc:")) {
return DbType.edbc;
} else if (rawUrl.startsWith("jdbc:mimer:multi1:")) {
return DbType.mimer;
} else if (rawUrl.startsWith("jdbc:dm:")) {
return JdbcConstants.DM;
} else if (rawUrl.startsWith("jdbc:kingbase:") || rawUrl.startsWith("jdbc:kingbase8:")) {
return JdbcConstants.KINGBASE;
} else if (rawUrl.startsWith("jdbc:gbase:")) {
return JdbcConstants.GBASE;
} else if (rawUrl.startsWith("jdbc:xugu:")) {
return JdbcConstants.XUGU;
} else if (rawUrl.startsWith("jdbc:log4jdbc:")) {
return DbType.log4jdbc;
} else if (rawUrl.startsWith("jdbc:hive:")) {
return DbType.hive;
} else if (rawUrl.startsWith("jdbc:hive2:")) {
return DbType.hive;
} else if (rawUrl.startsWith("jdbc:phoenix:")) {
return DbType.phoenix;
} else if (rawUrl.startsWith("jdbc:kylin:")) {
return DbType.kylin;
} else if (rawUrl.startsWith("jdbc:elastic:")) {
return DbType.elastic_search;
} else if (rawUrl.startsWith("jdbc:clickhouse:")) {
return DbType.clickhouse;
} else if (rawUrl.startsWith("jdbc:presto:")) {
return DbType.presto;
} else if (rawUrl.startsWith("jdbc:trino:")) {
return DbType.trino;
} else if (rawUrl.startsWith("jdbc:inspur:")) {
return DbType.kdb;
} else if (rawUrl.startsWith("jdbc:polardb")) {
return DbType.polardb;
} else if (rawUrl.startsWith("jdbc:highgo:")) {
return DbType.highgo;
} else if (rawUrl.startsWith("jdbc:pivotal:greenplum:") || rawUrl.startsWith("jdbc:datadirect:greenplum:")) {
return DbType.greenplum;
} else if (rawUrl.startsWith("jdbc:opengauss:") || rawUrl.startsWith("jdbc:gaussdb:") || rawUrl.startsWith("jdbc:dws:iam:")) {
return DbType.gaussdb;
} else {
return null;
}
}
必须用到可以考虑去从配置的properties中去读配置属性