Skip to content

Commit

Permalink
Fix WebServer.getConnection()
Browse files Browse the repository at this point in the history
  • Loading branch information
katzyn committed Jan 15, 2022
1 parent 456c2d0 commit eb75633
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion h2/src/main/org/h2/Driver.java
Expand Up @@ -56,7 +56,7 @@ public Connection connect(String url, Properties info) throws SQLException {
if (url == null) {
throw DbException.getJdbcSQLException(ErrorCode.URL_FORMAT_ERROR_2, null, Constants.URL_FORMAT, null);
} else if (url.startsWith(Constants.START_URL)) {
return new JdbcConnection(url, info, null, null);
return new JdbcConnection(url, info, null, null, false);
} else if (url.equals(DEFAULT_URL)) {
return DEFAULT_CONNECTION.get();
} else {
Expand Down
7 changes: 6 additions & 1 deletion h2/src/main/org/h2/jdbc/JdbcConnection.java
Expand Up @@ -103,12 +103,17 @@ public class JdbcConnection extends TraceObject implements Connection, JdbcConne
* @param info of this connection
* @param user of this connection
* @param password for the user
* @param forbidCreation whether database creation is forbidden
* @throws SQLException on failure
*/
@SuppressWarnings("resource")
public JdbcConnection(String url, Properties info, String user, Object password) throws SQLException {
public JdbcConnection(String url, Properties info, String user, Object password, boolean forbidCreation)
throws SQLException {
try {
ConnectionInfo ci = new ConnectionInfo(url, info, user, password);
if (forbidCreation) {
ci.setProperty("FORBID_CREATION", "TRUE");
}
String baseDir = SysProperties.getBaseDir();
if (baseDir != null) {
ci.setBaseDir(baseDir);
Expand Down
9 changes: 5 additions & 4 deletions h2/src/main/org/h2/jdbcx/JdbcDataSource.java
Expand Up @@ -152,7 +152,7 @@ public void setLogWriter(PrintWriter out) {
@Override
public Connection getConnection() throws SQLException {
debugCodeCall("getConnection");
return new JdbcConnection(url, null, userName, StringUtils.cloneCharArray(passwordChars));
return new JdbcConnection(url, null, userName, StringUtils.cloneCharArray(passwordChars), false);
}

/**
Expand All @@ -169,7 +169,7 @@ public Connection getConnection(String user, String password)
if (isDebugEnabled()) {
debugCode("getConnection(" + quote(user) + ", \"\")");
}
return new JdbcConnection(url, null, user, password);
return new JdbcConnection(url, null, user, password, false);
}

/**
Expand Down Expand Up @@ -319,7 +319,7 @@ public Reference getReference() {
public XAConnection getXAConnection() throws SQLException {
debugCodeCall("getXAConnection");
return new JdbcXAConnection(factory, getNextId(XA_DATA_SOURCE),
new JdbcConnection(url, null, userName, StringUtils.cloneCharArray(passwordChars)));
new JdbcConnection(url, null, userName, StringUtils.cloneCharArray(passwordChars), false));
}

/**
Expand All @@ -336,7 +336,8 @@ public XAConnection getXAConnection(String user, String password)
if (isDebugEnabled()) {
debugCode("getXAConnection(" + quote(user) + ", \"\")");
}
return new JdbcXAConnection(factory, getNextId(XA_DATA_SOURCE), new JdbcConnection(url, null, user, password));
return new JdbcXAConnection(factory, getNextId(XA_DATA_SOURCE),
new JdbcConnection(url, null, user, password, false));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions h2/src/main/org/h2/server/TcpServer.java
Expand Up @@ -86,7 +86,8 @@ private void initManagementDb() throws SQLException {
managementPassword = StringUtils.convertBytesToHex(MathUtils.secureRandomBytes(32));
}
// avoid using the driver manager
JdbcConnection conn = new JdbcConnection("jdbc:h2:" + getManagementDbName(port), null, "", managementPassword);
JdbcConnection conn = new JdbcConnection("jdbc:h2:" + getManagementDbName(port), null, "", managementPassword,
false);
managementDb = conn;

try (Statement stat = conn.createStatement()) {
Expand Down Expand Up @@ -446,7 +447,7 @@ public static synchronized void shutdown(String url, String password,
}
String db = getManagementDbName(port);
for (int i = 0; i < 2; i++) {
try (JdbcConnection conn = new JdbcConnection("jdbc:h2:" + url + '/' + db, null, "", password)) {
try (JdbcConnection conn = new JdbcConnection("jdbc:h2:" + url + '/' + db, null, "", password, true)) {
PreparedStatement prep = conn.prepareStatement("CALL STOP_SERVER(?, ?, ?)");
prep.setInt(1, all ? 0 : port);
prep.setString(2, password);
Expand Down
10 changes: 2 additions & 8 deletions h2/src/main/org/h2/server/web/WebServer.java
Expand Up @@ -801,16 +801,10 @@ Connection getConnection(String driver, String databaseUrl, String user,
String password, String userKey, NetworkConnectionInfo networkConnectionInfo) throws SQLException {
driver = driver.trim();
databaseUrl = databaseUrl.trim();
if (databaseUrl.startsWith("jdbc:h2:")) {
if (!allowSecureCreation || key == null || !key.equals(userKey)) {
if (ifExists) {
databaseUrl += ";FORBID_CREATION=TRUE";
}
}
}
// do not trim the password, otherwise an
// encrypted H2 database with empty user password doesn't work
return JdbcUtils.getConnection(driver, databaseUrl, user.trim(), password, networkConnectionInfo);
return JdbcUtils.getConnection(driver, databaseUrl, user.trim(), password, networkConnectionInfo,
ifExists && (!allowSecureCreation || key == null || !key.equals(userKey)));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions h2/src/main/org/h2/tools/CreateCluster.java
Expand Up @@ -102,7 +102,7 @@ private static void process(String urlSource, String urlTarget,
String user, String password, String serverList) throws SQLException {
// use cluster='' so connecting is possible
// even if the cluster is enabled
try (JdbcConnection connSource = new JdbcConnection(urlSource + ";CLUSTER=''", null, user, password);
try (JdbcConnection connSource = new JdbcConnection(urlSource + ";CLUSTER=''", null, user, password, false);
Statement statSource = connSource.createStatement()) {
// enable the exclusive mode and close other connections,
// so that data can't change while restoring the second database
Expand All @@ -120,7 +120,7 @@ private static void performTransfer(Statement statSource, String urlTarget, Stri
String serverList) throws SQLException {

// Delete the target database first.
try (JdbcConnection connTarget = new JdbcConnection(urlTarget + ";CLUSTER=''", null, user, password);
try (JdbcConnection connTarget = new JdbcConnection(urlTarget + ";CLUSTER=''", null, user, password, false);
Statement statTarget = connTarget.createStatement()) {
statTarget.execute("DROP ALL OBJECTS DELETE FILES");
}
Expand All @@ -129,7 +129,7 @@ private static void performTransfer(Statement statSource, String urlTarget, Stri
Future<?> threadFuture = startWriter(pipeReader, statSource);

// Read data from pipe reader, restore on target.
try (JdbcConnection connTarget = new JdbcConnection(urlTarget, null, user, password);
try (JdbcConnection connTarget = new JdbcConnection(urlTarget, null, user, password, false);
Statement statTarget = connTarget.createStatement()) {
RunScript.execute(connTarget, pipeReader);

Expand Down
2 changes: 1 addition & 1 deletion h2/src/main/org/h2/tools/GUIConsole.java
Expand Up @@ -464,7 +464,7 @@ private void createDatabase() {
}
String url = "jdbc:h2:" + path;
try {
new JdbcConnection(url, null, user, password).close();
new JdbcConnection(url, null, user, password, false).close();
errorArea.setForeground(new Color(0, 0x99, 0));
errorArea.setText("Database was created successfully.\n\n"
+ "JDBC URL for H2 Console:\n"
Expand Down
2 changes: 1 addition & 1 deletion h2/src/main/org/h2/tools/Upgrade.java
Expand Up @@ -163,7 +163,7 @@ public static boolean upgrade(String url, Properties info, int version) throws E
unloadH2(driver);
}
rename(name, false);
try (JdbcConnection conn = new JdbcConnection(url, info, null, null)) {
try (JdbcConnection conn = new JdbcConnection(url, info, null, null, false)) {
StringBuilder builder = StringUtils.quoteStringSQL(new StringBuilder("RUNSCRIPT FROM "), script)
.append(scriptCommandSuffix);
if (version <= 200) {
Expand Down
7 changes: 4 additions & 3 deletions h2/src/main/org/h2/util/JdbcUtils.java
Expand Up @@ -267,7 +267,7 @@ public static void closeSilently(ResultSet rs) {
*/
public static Connection getConnection(String driver, String url,
String user, String password) throws SQLException {
return getConnection(driver, url, user, password, null);
return getConnection(driver, url, user, password, null, false);
}

/**
Expand All @@ -278,13 +278,14 @@ public static Connection getConnection(String driver, String url,
* @param user the user name or {@code null}
* @param password the password or {@code null}
* @param networkConnectionInfo the network connection information, or {@code null}
* @param forbidCreation whether database creation is forbidden
* @return the database connection
* @throws SQLException on failure
*/
public static Connection getConnection(String driver, String url, String user, String password,
NetworkConnectionInfo networkConnectionInfo) throws SQLException {
NetworkConnectionInfo networkConnectionInfo, boolean forbidCreation) throws SQLException {
if (url.startsWith(Constants.START_URL)) {
JdbcConnection connection = new JdbcConnection(url, null, user, password);
JdbcConnection connection = new JdbcConnection(url, null, user, password, forbidCreation);
if (networkConnectionInfo != null) {
connection.getSession().setNetworkConnectionInfo(networkConnectionInfo);
}
Expand Down
6 changes: 6 additions & 0 deletions h2/src/test/org/h2/test/unit/TestTools.java
Expand Up @@ -538,6 +538,12 @@ private void testJdbcDriverUtils() {
assertEquals("08001", e.getSQLState());
assertEquals("Only java scheme is supported for JNDI lookups", e.getMessage());
}
try {
JdbcUtils.getConnection("org.h2.Driver", "jdbc:h2:mem:", "sa", "", null, true);
fail("Expected SQLException: " + ErrorCode.REMOTE_DATABASE_NOT_FOUND_1);
} catch (SQLException e) {
assertEquals(ErrorCode.REMOTE_DATABASE_NOT_FOUND_1, e.getErrorCode());
}
}

private void testWrongServer() throws Exception {
Expand Down

0 comments on commit eb75633

Please sign in to comment.