提交 d3bb6c84 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Detect unsupported URLs for the driver in JdbcUtils.getConnection()

上级 1fc1d43e
...@@ -21,6 +21,8 @@ Change Log ...@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>Issue #1730: console can no longer connect to psql dbs in v1.4.197 (works in v1.4.196)
</li>
<li>Issue #1590: Error on executing "DELETE FROM table1 WHERE ID = ?; DELETE FROM table2 WHERE ID = ?;" <li>Issue #1590: Error on executing "DELETE FROM table1 WHERE ID = ?; DELETE FROM table2 WHERE ID = ?;"
</li> </li>
<li>Issue #1727: Support ISODOW as identifier for the extract function additional to ISO_DAY_OF_WEEK <li>Issue #1727: Support ISODOW as identifier for the extract function additional to ISO_DAY_OF_WEEK
......
...@@ -288,17 +288,24 @@ public class JdbcUtils { ...@@ -288,17 +288,24 @@ public class JdbcUtils {
JdbcUtils.load(url); JdbcUtils.load(url);
} else { } else {
Class<?> d = loadUserClass(driver); Class<?> d = loadUserClass(driver);
if (java.sql.Driver.class.isAssignableFrom(d)) { try {
try { if (java.sql.Driver.class.isAssignableFrom(d)) {
Driver driverInstance = (Driver) d.getDeclaredConstructor().newInstance(); Driver driverInstance = (Driver) d.getDeclaredConstructor().newInstance();
return driverInstance.connect(url, prop); /*fix issue #695 with drivers with the same /*
jdbc subprotocol in classpath of jdbc drivers (as example redshift and postgresql drivers)*/ * fix issue #695 with drivers with the same jdbc
} catch (Exception e) { * subprotocol in classpath of jdbc drivers (as example
throw DbException.toSQLException(e); * redshift and postgresql drivers)
} */
} else if (javax.naming.Context.class.isAssignableFrom(d)) { Connection connection = driverInstance.connect(url, prop);
// JNDI context if (connection != null) {
try { return connection;
}
/*
* URL is not valid for the specified driver, fallback to
* default.
*/
} else if (javax.naming.Context.class.isAssignableFrom(d)) {
// JNDI context
Context context = (Context) d.getDeclaredConstructor().newInstance(); Context context = (Context) d.getDeclaredConstructor().newInstance();
DataSource ds = (DataSource) context.lookup(url); DataSource ds = (DataSource) context.lookup(url);
String user = prop.getProperty("user"); String user = prop.getProperty("user");
...@@ -307,13 +314,11 @@ public class JdbcUtils { ...@@ -307,13 +314,11 @@ public class JdbcUtils {
return ds.getConnection(); return ds.getConnection();
} }
return ds.getConnection(user, password); return ds.getConnection(user, password);
} catch (Exception e) {
throw DbException.toSQLException(e);
} }
} else { } catch (Exception e) {
// don't know, but maybe it loaded a JDBC Driver throw DbException.toSQLException(e);
return DriverManager.getConnection(url, prop);
} }
// don't know, but maybe it loaded a JDBC Driver
} }
return DriverManager.getConnection(url, prop); return DriverManager.getConnection(url, prop);
} }
......
...@@ -510,12 +510,15 @@ public class TestTools extends TestDb { ...@@ -510,12 +510,15 @@ public class TestTools extends TestDb {
} }
private void testJdbcDriverUtils() { private void testJdbcDriverUtils() {
assertEquals("org.h2.Driver", assertEquals("org.h2.Driver", JdbcUtils.getDriver("jdbc:h2:~/test"));
JdbcUtils.getDriver("jdbc:h2:~/test")); assertEquals("org.postgresql.Driver", JdbcUtils.getDriver("jdbc:postgresql:test"));
assertEquals("org.postgresql.Driver", assertEquals(null, JdbcUtils.getDriver("jdbc:unknown:test"));
JdbcUtils.getDriver("jdbc:postgresql:test")); try {
assertEquals(null, JdbcUtils.getConnection("org.h2.Driver", "jdbc:h2x:test", "sa", "");
JdbcUtils.getDriver("jdbc:unknown:test")); fail("Expected SQLException: No suitable driver found");
} catch (SQLException e) {
// OK
}
} }
private void testWrongServer() throws Exception { private void testWrongServer() throws Exception {
......
...@@ -807,4 +807,4 @@ analyst occupation distributive josaph aor engineer sajeewa isuru randil kevin d ...@@ -807,4 +807,4 @@ analyst occupation distributive josaph aor engineer sajeewa isuru randil kevin d
corrupts splitted disruption unintentional octets preconditions predicates subq objectweb insn opcodes corrupts splitted disruption unintentional octets preconditions predicates subq objectweb insn opcodes
preserves masking holder unboxing avert iae transformed subtle reevaluate exclusions subclause ftbl rgr preserves masking holder unboxing avert iae transformed subtle reevaluate exclusions subclause ftbl rgr
presorted inclusion contexts aax mwd percentile cont interpolate mwa hypothetical regproc childed listagg foreground presorted inclusion contexts aax mwd percentile cont interpolate mwa hypothetical regproc childed listagg foreground
isodow isoyear isodow isoyear psql
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论