DatabaseConnection implements AutoCloseable v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 23 Dec 2013 16:29:51 +0100
branchv_0
changeset 42 6fdaa4db3943
parent 41 514df5061f59
child 43 2813d3409afd
DatabaseConnection implements AutoCloseable
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Mon Dec 23 16:19:21 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Mon Dec 23 16:29:51 2013 +0100
@@ -109,9 +109,10 @@
 			if (fd == null) {
 				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
 			} else {
-				DatabaseConnection c = dd.connect();
-				Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
-				c.executeQuery(options.getSQLCommand(), f);
+				try (DatabaseConnection c = dd.connect()) {
+					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
+					c.executeQuery(options.getSQLCommand(), f);
+				}
 			}
 		}
 	}
--- a/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Mon Dec 23 16:19:21 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/DatabaseConnection.java	Mon Dec 23 16:29:51 2013 +0100
@@ -31,7 +31,7 @@
  *
  * @author Ing. František Kučera (frantovo.cz)
  */
-public class DatabaseConnection {
+public class DatabaseConnection implements AutoCloseable {
 
 	private DatabaseDefinition databaseDefinition;
 	private Connection connection;
@@ -114,4 +114,9 @@
 		}
 
 	}
+
+	@Override
+	public void close() throws SQLException {
+		connection.close();
+	}
 }