Unverified 提交 4fec0303 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1731 from katzyn/console

Detect unsupported URLs for the driver in JdbcUtils.getConnection()
...@@ -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
......
...@@ -724,7 +724,6 @@ public class WebServer implements Service { ...@@ -724,7 +724,6 @@ public class WebServer implements Service {
String password) throws SQLException { String password) throws SQLException {
driver = driver.trim(); driver = driver.trim();
databaseUrl = databaseUrl.trim(); databaseUrl = databaseUrl.trim();
org.h2.Driver.load();
Properties p = new Properties(); Properties p = new Properties();
p.setProperty("user", user.trim()); p.setProperty("user", user.trim());
// do not trim the password, otherwise an // do not trim the password, otherwise an
...@@ -734,19 +733,7 @@ public class WebServer implements Service { ...@@ -734,19 +733,7 @@ public class WebServer implements Service {
if (ifExists) { if (ifExists) {
databaseUrl += ";IFEXISTS=TRUE"; databaseUrl += ";IFEXISTS=TRUE";
} }
// PostgreSQL would throw a NullPointerException
// if it is loaded before the H2 driver
// because it can't deal with non-String objects in the connection
// Properties
return org.h2.Driver.load().connect(databaseUrl, p);
} }
// try {
// Driver dr = (Driver) urlClassLoader.
// loadClass(driver).newInstance();
// return dr.connect(url, p);
// } catch(ClassNotFoundException e2) {
// throw e2;
// }
return JdbcUtils.getConnection(driver, databaseUrl, p); return JdbcUtils.getConnection(driver, databaseUrl, p);
} }
......
...@@ -288,17 +288,21 @@ public class JdbcUtils { ...@@ -288,17 +288,21 @@ 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)
*/
Connection connection = driverInstance.connect(url, prop);
if (connection != null) {
return connection;
} }
throw new SQLException("Driver " + driver + " is not suitable for " + url, "08001");
} else if (javax.naming.Context.class.isAssignableFrom(d)) { } else if (javax.naming.Context.class.isAssignableFrom(d)) {
// JNDI context // JNDI context
try {
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 +311,11 @@ public class JdbcUtils { ...@@ -307,13 +311,11 @@ public class JdbcUtils {
return ds.getConnection(); return ds.getConnection();
} }
return ds.getConnection(user, password); return ds.getConnection(user, password);
}
} catch (Exception e) { } catch (Exception e) {
throw DbException.toSQLException(e); throw DbException.toSQLException(e);
} }
} else {
// don't know, but maybe it loaded a JDBC Driver // don't know, but maybe it loaded a JDBC Driver
return DriverManager.getConnection(url, prop);
}
} }
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: 08001");
} catch (SQLException e) {
assertEquals("08001", e.getSQLState());
}
} }
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论