From: Chris Jaekl Date: Thu, 13 Oct 2016 11:42:11 +0000 (+0900) Subject: Opens and closes the JDBC connection for each command X-Git-Url: https://jaekl.net/gitweb/?a=commitdiff_plain;h=4ddb397a3f02b0cedeee070b8b10b1a8fa2c8ff3;p=squelch.git Opens and closes the JDBC connection for each command Instead of holding the connection open for the duration of the application, open a fresh connection for each command, and close it immediately after the command completes. This may reduce the risk of having connections hang around for a while, if the JVM terminates unexpectedly (e.g., if the ssh session gets killed and the console goes away). --- diff --git a/pom.xml b/pom.xml index 04dc979..1db3050 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ net.jaekl.squelch squelch jar - 0.1 + 0.1a-SNAPSHOT squelch http://maven.apache.org diff --git a/src/main/java/net/jaekl/squelch/Squelch.java b/src/main/java/net/jaekl/squelch/Squelch.java index 848ede7..7b43bd2 100644 --- a/src/main/java/net/jaekl/squelch/Squelch.java +++ b/src/main/java/net/jaekl/squelch/Squelch.java @@ -100,35 +100,32 @@ public class Squelch { String jdbcUrl = m_args.getUrl(); DbDriver driver = getDriverFor(jdbcUrl); - try (Connection conn = getConnection(driver, jdbcUrl)) - { - while (true) { - boolean processed = false; - line = ci.readLine(PROMPT); - - for (Stmt statement : m_statements) { - if (statement.handles(line)) { - try { - statement.exec(driver, conn, pw, line); - } - catch (SQLException exc) { - exc.printStackTrace(pw); - } - processed = true; - break; + while (true) { + boolean processed = false; + line = ci.readLine(PROMPT); + + for (Stmt statement : m_statements) { + if (statement.handles(line)) { + try (Connection conn = getConnection(driver, jdbcUrl)){ + statement.exec(driver, conn, pw, line); } - } - - if ((!processed)) { - if (isQuit(line)) { - break; + catch (SQLException exc) { + exc.printStackTrace(pw); } - // Unrecognized command - // TODO: add a string table, and a natural-language error message. - pw.println("??? \"" + line + "\""); + processed = true; + break; + } + } + + if ((!processed)) { + if (isQuit(line)) { + break; } - pw.flush(); + // Unrecognized command + // TODO: add a string table, and a natural-language error message. + pw.println("??? \"" + line + "\""); } + pw.flush(); } } }