X-Received: by 2002:a37:984:: with SMTP id 126mr697524qkj.3.1551382456840; Thu, 28 Feb 2019 11:34:16 -0800 (PST) X-Received: by 2002:a25:c304:: with SMTP id t4mr1026357ybf.359.1551382456667; Thu, 28 Feb 2019 11:34:16 -0800 (PST) Path: csiph.com!4.us.feeder.erje.net!feeder.erje.net!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!m21no5194765qta.0!news-out.google.com!y15ni2305qta.0!nntp.google.com!m21no5194759qta.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Thu, 28 Feb 2019 11:34:16 -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: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: it's Closeable, but I don't want to close() it yet. From: Eric Douglas Injection-Date: Thu, 28 Feb 2019 19:34:16 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Lines: 18 Xref: csiph.com comp.lang.java.programmer:38756 On Thursday, February 28, 2019 at 2:10:32 PM UTC-5, Andreas Leitgeb wrote: > I totally agree for a typical "Pool" scenario. >=20 > My case can be better characterized in that the Closeable entity is > an attribute of a Context class, and a getter is called by "users" for > the ref of the entity. Lifetime management of the entity is handled > by the Context. "Users" (specific methods doing their parts of > the Context's whole task) really shouldn't close() it or they would > spoil it for later users. I did mention if you're pooling it, don't open it. If you declare a Closea= ble as a class level variable you can open it once then use it many times w= ith that instance. I have a class level java.sql.Connection which I may op= en in the constructor, but if I put the open call inside a method Eclipse s= till doesn't complain if it doesn't get closed. If you only want to open i= t when the method gets called, initialize it to null. Then the method can = say if (conn=3D=3Dnull){conn=3DDriverManager.getConnection(connectstring)}.= Remember to synchronize it if the method may be used by multiple threads = simultaneously.