Opens and closes the JDBC connection for each command
authorChris Jaekl <cejaekl@yahoo.com>
Thu, 13 Oct 2016 11:42:11 +0000 (20:42 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Thu, 13 Oct 2016 11:42:11 +0000 (20:42 +0900)
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
src/main/java/net/jaekl/squelch/Squelch.java

diff --git a/pom.xml b/pom.xml
index 04dc9793ea9d1ea2cbe2dbe2eff1226f79c015b4..1db305055a853dae200a0f78e445a0a1fda46ae1 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
   <groupId>net.jaekl.squelch</groupId>
   <artifactId>squelch</artifactId>
   <packaging>jar</packaging>
-  <version>0.1</version>
+  <version>0.1a-SNAPSHOT</version>
   <name>squelch</name>
   <url>http://maven.apache.org</url>
   <build>
index 848ede7db05c9a5d88c783f66bd250a7c4211e55..7b43bd21746170c0fd322ff3a5c73a974ef53d5f 100644 (file)
@@ -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();
                }
        }       
 }