Skip to content

Commit

Permalink
Enhance the JDBC Driver Manager to validate all driver supported
Browse files Browse the repository at this point in the history
properties and remove the unsupported from the connection properties
  • Loading branch information
speckyspooky committed Aug 3, 2024
1 parent 7585397 commit 11ccfb4
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ private Connection doConnect(String driverClass, String url, String jndiNameUrl,
try {
Driver driver = DriverManager.getDriver(url);
if (driver != null) {
removeUnsupportedConnectionProperties(driver, url, connectionProperties);
return driver.connect(url, connectionProperties);
}
} catch (SQLException e1) {
Expand All @@ -292,6 +293,7 @@ private Connection doConnect(String driverClass, String url, String jndiNameUrl,
}

try {
removeUnsupportedConnectionProperties(DriverManager.getDriver(url), url, connectionProperties);
return DriverManager.getConnection(url, connectionProperties);
} catch (SQLException e) {
try {
Expand All @@ -301,7 +303,9 @@ private Connection doConnect(String driverClass, String url, String jndiNameUrl,

Class dc = dl.loadClass(driverClass);
if (dc != null) {
Connection conn = ((Driver) dc.newInstance()).connect(url, connectionProperties);
Driver driver = (Driver) dc.newInstance();
removeUnsupportedConnectionProperties(driver, url, connectionProperties);
Connection conn = driver.connect(url, connectionProperties);
if (conn != null) {
return conn;
}
Expand All @@ -313,6 +317,28 @@ private Connection doConnect(String driverClass, String url, String jndiNameUrl,
}
}

/*
* Remove unsupported properties from the connection properties
* based on the driver property information set
*/
private void removeUnsupportedConnectionProperties(Driver driver, String url, Properties connectionProperties) {
try {
DriverPropertyInfo[] supportedProperties = driver.getPropertyInfo(url, connectionProperties);
if (supportedProperties != null && connectionProperties != null) {
connectionProperties.keySet().removeIf(property -> {
String key = property.toString();
for (DriverPropertyInfo supportedProperty : supportedProperties) {
if (supportedProperty.name.equalsIgnoreCase(key)) {
return false;
}
}
return true;
});
}
} catch (SQLException e) {
// do nothing, standard handling
}
}
/**
*
* @param message
Expand Down

0 comments on commit 11ccfb4

Please sign in to comment.