Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Not closing connections and statements Date: Fri, 12 Aug 2016 13:59:37 +0200 Organization: Decebal Computing Lines: 38 Message-ID: <87shuakxjq.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="12baf5125b19322b899c79c2e3878015"; logging-data="14837"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/Zd8RD/FvhyxLDgg5XQ7ttHySP89hFto=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:Y2mmQs5Jc2GaQhNX18koQQvf4RI= sha1:Xui1xNQAfnInjmomaiWmr1DXb7I= Xref: csiph.com comp.lang.java.programmer:35268 As I understood it you should close your SQL connections and statements before quitting a program. How bad is this when that does not happen? At the moment I am writing a general function to log memory usage to a SQLite database to be used by different applications. How bad would it be if a program would exit without cleanly closing the connection and statement? The beginning of my code is at the moment: public static boolean freeMemoryThread(final String className, final String database) { final long divideValue = 1024 * 1024; final Runtime rt = Runtime.getRuntime(); final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); Connection conn = null; ResultSet rs = null; Statement stmt = null; logMessage(className + ", " + database); try { Class.forName("org.sqlite.JDBC"); conn = DriverManager.getConnection("jdbc:sqlite:" + database); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT SQLITE_VERSION() AS SQLiteVersion;"); rs.next(); logMessage(String.format("Using SQLite %s", rs.getString("SQLiteVersion"))); rs = null; } catch (Exception e) { logMessage("Could not connect with the database: " + e.getMessage()); return false; } ses.scheduleAtFixedRate(new Runnable() { -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof