驱动程序管理器没有为数据库使用正确的驱动程序 url

Driver manager not using the correct driver for the DB url

我有一个 java 应用程序,它可以创建到多个数据库的连接。

我们正在加载这些驱动程序:

Class.forName("org.mariadb.jdbc.Driver");
Class.forName("com.treasure_data.jdbc.TreasureDataDriver");

当我尝试连接到 aurora DB 时,我希望 DriverManager 使用 MariaDB 驱动程序 - 但它使用的是 treasure_data 驱动程序。

java.sql.Connection conn1 = DriverManager.getConnection("jdbc:mysql:aurora://YYY-aurora.XXXXX.com:3306/SomeDBName", "USER", "PASSWORD");

这是我得到的错误:

java.sql.SQLException: Invalid JDBC URL: jdbc:mysql:aurora://YYY-aurora.XXXXX.com:3306/SomeDBName. URL prefix must be jdbc:td://

为什么 DriverManager 使用 treasure_data 驱动程序?

java.sql.DriverManager 将要求 每个 注册驱动程序创建与 JDBC url 的连接,直到有一个驱动程序 returns a (non-null) java.sql.Connection 或抛出 SQLException.

来自 JDBC 4.2 节 9.2 驱动程序接口:

When the DriverManager is trying to establish a connection, it calls that driver's connect method and passes the driver the URL. If the Driver implementation understands the URL, it will return a Connection object or throw a SQLException if a connection cannot be maded to the database. If the Driver implementation does not understand the URL, it will return null.

来自Driver.connect API doc:

Attempts to make a database connection to the given URL. The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the JDBC driver manager is asked to connect to a given URL it passes the URL to each loaded driver in turn.

The driver should throw an SQLException if it is the right driver to connect to the given URL but has trouble connecting to the database.

所以一个兼容的 JDBC 驱动程序 必须 return null 当它被要求为 JDBC [创建连接时=53=] 它没有 recognize/support。只有当支持 URL 时(例如,因为前缀与驱动程序匹配),驱动程序才允许抛出 SQLException(例如,当 JDBC 的其余部分存在语法错误时URL,或者无法建立连接)。

所以 com.treasure_data.jdbc.TreasureDataDriver 是错误的,因为它为它无法识别的 URL 抛出 SQLException;您需要向该驱动程序的作者提交错误报告。