开发技巧|SpringBoot中连接oracle出现告警信息

2023-11-21 10:48:07 浏览数 (1)

问题出现的背景和原因

前几日领导说需要连接友商的Oracle,然后读取友商的数据展示到页面来。工作还是需要做的嘛,虽然比较机械些,但是工作量还是有的(此处划水摸鱼了呢?)。为了演示连接成功和可以读取数据,我在本地搭建了一个Oracle,然后作为测试来模拟读取,后续只需要替换下连接器的连接即可。然后,在启动SpringBoot项目时,有一个很显眼的WARN红色字体在控制台打印出来了。作为一个患有强迫症的开发者来说,此处绝对不能有~

WARN com.alibaba.druid.pool.DruidAbstractDataSource - oracle.jdbc.driver.OracleDriver is deprecated.Having use oracle.jdbc.OracleDriver

当前我Oracle服务器的数据库版本为:11.2g ,此处画上一个重点,后续有解答。

根据问题着手处理

虽然这个不影响项目的运行,但是出于看着不舒服的情况,我还是决定看看这个告警出现的问题。仔细一看,这个问题还是比较简单的。我找翻译器给翻译下吧。

看出来了吧,是不是已经知道什么问题了?大白话就是在使用驱动连接器的类过期了,不在使用oracle.jdbc.driver.OracleDriver 这个驱动器类了,而是改为了oracle.jdbc.OracleDriver。相比以前好像是简洁了一个单词,少了一个也是一种进步,一种优化。看来我是很久都不用了。

思考的路永远欢迎你踏上去

为了增加我猜测的准确性,我决定还是搜索下吧,下面看下官方提供的解释,我认为这个应该是比较准确了。

官方解释:

代码语言:javascript复制
Package oracle.jdbc
Beginning in Oracle9i, the Oracle extensions to JDBC are captured in the package oracle.jdbc. This package contains classes and interfaces that specify the Oracle extensions in a manner similar to the way the classes and interfaces in java.sql specify the public JDBC API.
Your code should use the package oracle.jdbc instead of the package oracle.jdbc.driver used in earlier versions of Oracle. Use of the package oracle.jdbc.driver is now deprecated, but will continue to be supported for backwards compatibility.
All that is required to covert your code is to replace "oracle.jdbc.driver" with "oracle.jdbc" in the source and recompile. This cannot be done piece-wise. You must convert all classes and interfaces that are referenced by an application. Conversion is not required, but is highly recommended. Future releases of Oracle may have features that are incompatible with use of the package oracle.jdbc.driver.
The purpose of this change is to enable the Oracle JDBC drivers to have multiple implementations. In all releases up to and including Oracle9i, all of the Oracle JDBC drivers have used the same top level implementation classes, the classes in the package oracle.jdbc.driver. By converting your code to use oracle.jdbc, you will be able to take advantage of future enhancements that use different implementation classes. There are no such enhancements in Oracle9i, but there are plans for such enhancements in the future.
Additionally, these interfaces permit the use of some code patterns that are difficult to use when your code uses the package oracle.jdbc.driver. For example, you can more easily develop wrapper classes for the Oracle JDBC classes. If you wished to wrap the OracleStatement class in order to log all SQL statements, you could easily do so by creating a class that wraps OracleStatement. That class would implement the interface oracle.jdbc.OracleStatement and hold an oracle.jdbc.OracleStatement as an instance variable. This wrapping pattern is much more difficult when your code uses the package oracle.jdbc.driver as you cannot extend the class oracle.jdbc.driver.OracleStatement.
Once again, your code should use the new package oracle.jdbc instead of the package oracle.jdbc.driver. Conversion is not required as oracle.jdbc.driver will continue to be supported for backwards compatibility. Conversion is highly recommended as there may in later releases be features that are not supported if your code uses oracle.jdbc.driver.
Since:
9i
See Also:
java.sql

这篇文章的地址来源于:https://docs.oracle.com/en/database/oracle/oracle-database/21/jajdb/oracle/jdbc/package-summary.html 有兴趣的小伙伴可以阅读,可以提升下英文阅读能力。

综合上述可见,从oracle9i开始,使用oracle.jdbc.OracleDriver代替oracle.jdbc.driver.OracleDriver。如果继续使用oracle.jdbc.driver.OracleDriver,在后续可能出现不支持某些功能。oracle.jdbc.OracleDriver 继承oracle.jdbc.driver.OracleDriver,是为了与老版本兼容,这也就可以说明我这里出现问题的原因了。

解决办法总比问题多

先看下我原来的在YML中配置multi-second-oracle的驱动信息

代码语言:javascript复制
# 省略……
      datasource:
        master:
          url: jdbc:mysql://[IP]:[PORT]/[dbName]?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
          username: [Username]
          password: [Password]
          driver-class-name: com.mysql.cj.jdbc.Driver
        # 多数据源配置
        multi-second-oracle:
          url: jdbc:oracle:thin:@[IP]:[PORT]:[Schema]
          username: [Username]
          password: [Password]
          driver-class-name: oracle.jdbc.driver.OracleDriver
# 省略……

将oracle.jdbc.driver.OracleDriver替换为oracle.jdbc.OracleDriver就可以了消除上面的告警信息了。

关于Oracle的工具连接使用的驱动器类

说到这里,其实还有一种方式可以避免这种问题的出现。我们在使用工具连接数据库服务器的时候,填写完成必要信息后,选择数据库类型,此时,工具会根据你的数据库服务器的版本来选择最优的驱动连接器,此时的驱动器连接器的类名称便是你需要的连接驱动器类。

举一反三:关于MySQL的驱动器类

想到这里,其实,这个同我们在使用MySQL时是一样的道理。例如,我们在使用MySQL时,使用的驱动器为com.mysql.cj.jdbc.Driver,则代表我们连接的数据库服务器的版本为8.0 ,如果是com.mysql.jdbc.Driver则是8.0以下的MySQL版本。


我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

0 人点赞