Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Not closing connections and statements Date: Fri, 12 Aug 2016 21:58:14 -0400 Organization: Aioe.org NNTP Server Lines: 44 Message-ID: References: <87shuakxjq.fsf@Equus.decebal.nl> <87k2fllkjf.fsf@Equus.decebal.nl> NNTP-Posting-Host: 7SK6yTFfqwlIPNBLl3l7Qg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:35288 On 8/12/2016 5:55 PM, Cecil Westerhof wrote: > That is the case. As long as the program is running I need the > database connection. Only when the program is terminated the database > connection is not used anymore. I show the code at the end of this > message. > And the code: > public static boolean freeMemoryThread(final String className, final String database) { > final Connection conn; > final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); > try { > PreparedStatement ps; > ResultSet rs; > Statement stmt; > > Class.forName("org.sqlite.JDBC"); > conn = DriverManager.getConnection("jdbc:sqlite:" + database); > } catch (Exception e) { > logMessage("Could not connect with the database: " + e.getMessage()); > return false; > } > ses.scheduleAtFixedRate(new Runnable() { > @Override > public void run() { > try { > PreparedStatement ps = conn.prepareStatement(insertLine); > ps.executeUpdate(); > } catch (Exception e) { > logMessage("Could not write memory line: " + e.getMessage()); > } > } > }, 15, 60, TimeUnit.MINUTES); > return true; > } I would find it tempting to open the connection in main with a try resource and pass the connection object to this method. Arne