Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32440

Re: Why save the EntityManagerFactory

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Why save the EntityManagerFactory
Date 2015-08-30 21:55 -0400
Organization Aioe.org NNTP Server
Message-ID <ms0c6v$2hq$1@speranza.aioe.org> (permalink)
References <87a8tnr2kq.fsf@Equus.decebal.nl> <mr6o7p$p9l$1@dont-email.me>

Show all headers | View raw


On 8/21/2015 4:43 AM, Eternal Hope wrote:
> 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.

Good point.

It could hold some resources.

Probably not likely as the only obvious system resource is database
connections and they should be in separate pool outside of EMF.

> em =
> Persistence.createEntityManagerFactory("JavaApplication12PU").createEntityManager();
> might leave an unclosed 'thing' consuming resources.

Yes. It will first be relased at some unknown point in time when GC hits it.

> 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

No. It will be collected no matter what it has references to as long
as nothing reachable has reference to 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.

Guess - you don't like fluent API's.

:-)

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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