1

I have a java application which is able create a connection to multiple DB.

We are loading these drivers:

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

When I try to connect to aurora DB I would expect DriverManager to use the MariaDB driver - but instead it is using treasure_data driver.

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

and this is the error I get:

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

Why is DriverManager using the treasure_data Driver?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
2Big2BeSmall
  • 1,348
  • 3
  • 20
  • 40

1 Answers1

1

The java.sql.DriverManager will ask each registered driver to create a connection with the JDBC url until one driver either returns a (non-null) java.sql.Connection or throws an SQLException.

From JDBC 4.2 section 9.2 The Driver interface:

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.

From the 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.

So a compliant JDBC driver must return null when it is asked to create connection for a JDBC URL it doesn't recognize/support. Only when the URL is supported (eg because the prefix matches the driver), is a driver allowed to throw an SQLException (eg when there is a syntax error in the rest of the JDBC URL, or if the connection cannot be established).

So the com.treasure_data.jdbc.TreasureDataDriver is misbehaving because it throws an SQLException for an URL it doesn't recognise; you need to file a bug report with the author of that driver.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197