Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32360 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2015-08-19 12:32 +0200 |
| Last post | 2015-08-31 10:13 -0700 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-08-19 12:32 +0200 |
| Subject | Why save the EntityManagerFactory |
| Message-ID | <87a8tnr2kq.fsf@Equus.decebal.nl> |
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();
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-08-19 19:15 -0400 |
| Message-ID | <mr32n2$9aq$2@speranza.aioe.org> |
| In reply to | #32360 |
On 8/19/2015 6:32 AM, 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();
If emf is not used later then I can not see any point.
In Java EE context you would typical let the container inject what you need.
Arne
[toc] | [prev] | [next] | [standalone]
| From | Eternal Hope <laughingspam99@gmail.com> |
|---|---|
| Date | 2015-08-21 09:43 +0100 |
| Message-ID | <mr6o7p$p9l$1@dont-email.me> |
| In reply to | #32360 |
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
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-08-30 21:55 -0400 |
| Message-ID | <ms0c6v$2hq$1@speranza.aioe.org> |
| In reply to | #32389 |
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
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2015-08-31 10:13 -0700 |
| Message-ID | <c58381ba-2f6a-4595-9299-f65e91ac28bd@googlegroups.com> |
| In reply to | #32440 |
On Sunday, August 30, 2015 at 6:56:08 PM UTC-7, Arne Vajhøj wrote:
> 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.
'EntityManagerFactory' is a thread-safe, heavyweight object. It's usually kept around because its job, as the name indicates, is to create 'EntityManager' instances. 'EntityManager' instances are not meant to linger but to serve brief sessions, but they're more lightweight and not thread safe. So you would have a lot of overhead if you keep creating 'EntityManagerFactory' instances.
You could think of "metasession" - sessions of sessions. An 'EntityManager' handles a relatively small unit of functionality. All related units could share a factory, and some other group could use another, But that's almost certainly overengineering.
I recommend having one 'EntityManagerFactory' shared around, and pop up/release 'EntityManager' instances as persistence needs come up.
Do not let 'EntityManager' instances linger around too long. They get cluttered with managed objects and you lose the ability to understand easily what's going on when performance falls through the abyss. Keep them lean and local.
--
Lew
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web