背景
项目中需要用到oracle数据库,原本框架是用的mysql。对配置进行了修改,如下:
spring:
datasource:
dynamic:
datasource:
master:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@*.*.*.*:1521:orcl
username: ***
password: ***
但是启动时报错,报错信息如下:
java.sql.SQLException: not support oracle driver 5.1
at com.alibaba.druid.pool.DruidDataSource.initCheck(DruidDataSource.java:1215) ~[druid-1.1.20.jar:1.1.20]
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:898) ~[druid-1.1.20.jar:1.1.20]
at com.worktrans.core.db.DynamicDataSourceCreator.createDruidDataSource(DynamicDataSourceCreator.java:215) [db-spring-boot-starter-2.0.0-20200115.103525-1.jar:na]
at com.worktrans.core.db.DynamicDataSourceCreator.createDataSource(DynamicDataSourceCreator.java:100) [db-spring-boot-starter-2.0.0-20200115.103525-1.jar:na]
at com.worktrans.core.db.provider.DefaultDynamicDataSourceProvider.loadDataSources(DefaultDynamicDataSourceProvider.java:41) [db-spring-boot-starter-2.0.0-20200115.103525-1.jar:na]
at com.worktrans.core.db.DynamicRoutingDataSource.init(DynamicRoutingDataSource.java:134) [db-spring-boot-starter-2.0.0-20200115.103525-1.jar:na]
....................余下就不放了
查了下网上的做法,说换个版本,但是我换了还是报错,只好进行debug。
问题排查
根据异常信息,找到了判断的代码,如下:
protected void initCheck() throws SQLException {
if ("oracle".equals(this.dbType)) {
this.isOracle = true;
if (this.driver.getMajorVersion() < 10) {
throw new SQLException("not support oracle driver " + this.driver.getMajorVersion() + "." + this.driver.getMinorVersion());
}
if (this.driver.getMajorVersion() == 10 && this.isUseOracleImplicitCache()) {
this.getConnectProperties().setProperty("oracle.jdbc.FreeMemoryOnEnterImplicitCache", "true");
}
this.oracleValidationQueryCheck();
} else if ("db2".equals(this.dbType)) {
this.db2ValidationQueryCheck();
} else if ("mysql".equals(this.dbType) || "com.mysql.cj.jdbc.Driver".equals(this.dbType)) {
this.isMySql = true;
}
if (this.removeAbandoned) {
LOG.warn("removeAbandoned is true, not use in production.");
}
}
对代码debug后发现this.driver用的还是mysql的驱动,那么问题就很明显了,我的配置没有生效。询问了公司架构师,公司框架本身有一个默认的配置文件,配置文件中默认写了mysql的驱动,将它改成oracle的驱动类,重启就能正确加载了。