Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32389
| From | Eternal Hope <laughingspam99@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Why save the EntityManagerFactory |
| Date | 2015-08-21 09:43 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <mr6o7p$p9l$1@dont-email.me> (permalink) |
| References | <87a8tnr2kq.fsf@Equus.decebal.nl> |
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why save the EntityManagerFactory Cecil Westerhof <Cecil@decebal.nl> - 2015-08-19 12:32 +0200
Re: Why save the EntityManagerFactory Arne Vajhøj <arne@vajhoej.dk> - 2015-08-19 19:15 -0400
Re: Why save the EntityManagerFactory Eternal Hope <laughingspam99@gmail.com> - 2015-08-21 09:43 +0100
Re: Why save the EntityManagerFactory Arne Vajhøj <arne@vajhoej.dk> - 2015-08-30 21:55 -0400
Re: Why save the EntityManagerFactory Lew <lewbloch@gmail.com> - 2015-08-31 10:13 -0700
csiph-web