X-Received: by 2002:ac8:1997:: with SMTP id u23mr4689734qtj.11.1551363548135; Thu, 28 Feb 2019 06:19:08 -0800 (PST) X-Received: by 2002:a25:2591:: with SMTP id l139mr6815859ybl.481.1551363547958; Thu, 28 Feb 2019 06:19:07 -0800 (PST) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!m21no4547558qta.0!news-out.google.com!o7ni1760qta.1!nntp.google.com!m21no4547554qta.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Thu, 28 Feb 2019 06:19:07 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.78.95.177; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H NNTP-Posting-Host: 50.78.95.177 References: <709a1f3b-fc99-4bd0-a8b8-866092bb7ae9@googlegroups.com> <8e1597a2-5c18-4fd1-83a4-7667e4d11e77@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <23a8128b-5c54-45ba-bfd0-2f7dda078517@googlegroups.com> Subject: Re: it's Closeable, but I don't want to close() it yet. From: Eric Douglas Injection-Date: Thu, 28 Feb 2019 14:19:08 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Lines: 39 Xref: csiph.com comp.lang.java.programmer:38747 On Thursday, February 28, 2019 at 7:20:35 AM UTC-5, Andreas Leitgeb wrote: > The Closeable interface does not provide any concept of opening. > Eclipse just sees a local variable of a Closeable type that > goes out of scope without .close() being called on it. Whether Eclipse cares depends on the scope. I had the "Potential Resource Leak" error set to ignore because it was flag= ging potential leaks which should not be leaks. For example I had 2 method= s opening different kinds of InputStream, passing their streams into one co= mmon method to read them. The method reading the streams was guaranteed to = close the stream for them using a try-finally block, but I didn't see a way= to tell Eclipse that, so it complained about the methods which opened the = streams. The only way to avoid that warning is to put the method that open= s the stream in yet another try-finally block that attempts to close it aga= in. I don't know if this logic makes sense, or if the method that only rea= ds the streams should never close them, but it seems tedious. In another class where I really didn't want to close the resource, I opened= it in the constructor. It didn't complain about that. If you want to cre= ate one java.sql.Connection to use for multiple queries, declare it outside= of your method and there's no warning. It doesn't warn to close it if I o= pen it in the method and it's declared outside (class level variable). Obviously it's a big deal if you open multiple instances of java.sql.Connec= tion without closing them. I'm not sure the impact of the other classes. = In order to execute a query, I open the Connection, then I open a java.sql.= Statement, then I open a java.sql.ResultSet. Eclipse shows all 3 of these = require their own close. What happens if you don't close the Statement, do= es it eat up excess memory? Does it keep all query results in memory indef= initely if you don't close the ResultSet? These classes are AutoCloseable,= so you can do a try with resources to close them. Eclipse doesn't care if= they're closed if you declare them as class level, and doesn't expect them= to close if they're the return value, though I'm not sure about embedded v= alues. Can you create multiple closeable objects and pass them out in an a= rray or map? One way or the other, all closeable resources must get closed. If you have= another method to guarantee they get closed outside of where they're decla= red, the simple answer is suppress the warning.