From 4ddb397a3f02b0cedeee070b8b10b1a8fa2c8ff3 Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Thu, 13 Oct 2016 20:42:11 +0900 Subject: [PATCH] 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). --- pom.xml | 2 +- src/main/java/net/jaekl/squelch/Squelch.java | 47 +++++++++----------- 2 files changed, 23 insertions(+), 26 deletions(-) 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(); } } } -- 2.30.2