Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eternal Hope Newsgroups: comp.lang.java.programmer Subject: Re: Why save the EntityManagerFactory Date: Fri, 21 Aug 2015 09:43:17 +0100 Organization: A noiseless patient Spider Lines: 43 Message-ID: References: <87a8tnr2kq.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 21 Aug 2015 08:41:29 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="2ab430ef8c6b5deb5816c858366218e1"; logging-data="25909"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+AmLwXjIIfRYeGof5ipVyJ++VyF004c7Y=" User-Agent: Mozilla/5.0 (Windows NT 6.3; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: <87a8tnr2kq.fsf@Equus.decebal.nl> Cancel-Lock: sha1:JrmoHDvZ5QTBUyWzDSJrckKyOhM= Xref: csiph.com comp.lang.java.programmer:32389 On 19/08/2015 11:32, Cecil Westerhof wrote: > I am using http://www.uow.edu.au/~nabg/399/JPA/JPA.pdf to get a start > with JPA. When looking at page 14 an EntityManagerFactory is created > and kept until the end of the program. > Could the following: > emf = Persistence.createEntityManagerFactory("JavaApplication12PU"); > em = emf.createEntityManager(); > not just be changed to: > em = Persistence.createEntityManagerFactory("JavaApplication12PU").createEntityManager(); > Just a guess but private static void closedownDataPersistence() { em.close(); emf.close(); } implies to me that both em and emf consume system resources and require separately closing to release those resources when you are finished with them. em = Persistence.createEntityManagerFactory("JavaApplication12PU").createEntityManager(); might leave an unclosed 'thing' consuming resources. What is the status of the EntityManagerFactory when you return from your chained expression above? you certainly must create one but when you return you have no reference to the factory so can't close it. It won't be collected as, if it still has a handle on some system resource it won't be null. So you are left with a zombie Object consuming resources with no way to reclaim it. Also, long chains of method calls are harder to read so it's good practice to unchain them. Chaining isn't particularly clever although as they become more adept at the language all Java developers seem to do it to some extent or other. Like I say, just a guess. -- Laughing Spam Fritter