SOS!! Simple yes/no question about JPA...

Hello,

I have the following environment and requirements:

-Tomcat 5.5 (no ejb container)

-Latest version of Hibernate

-JSF 1.1

-A requirement to use JPA

-I must use the query cache and the second-level cache

My question is as follows:

What is the best solution?

Solution 1.

ONE EntityManagerFactory stored in the ServletContext for use by all of my web app users generating MULTIPLE INSTANCES of EntityManagers. (would this allow me to use the query cache?)

Solution 2.

ONE EntityManagerFactory and ONE EntityManager stored in the ServletContext for use by all of my web app users.

Thanks in advance,

Julien.

[728 byte] By [balteoa] at [2007-11-27 0:37:16]
# 1

Solution 1 is better, but for reasons having nothing to do with caching. EntityManager objects are

not intended to be concurrently accessed.In that respect they are like JDBC connections.You

should not have a situation where multiple threads try to invoke the same EntityManager object

concurrently. That's why you shouldn't have one global EntityManager for the web app.

Regarding caching, what exactly are you referring to by "query cache"? Are you saying you

plan to execute the same query multiple times but you'd like the underlying persistence manager

to avoid trips to the actual database?Whether the query is executed by an actual database

access or is fulfilled through some JVM-local cache is not controlled by the spec. Most implementations do allow for such caching but the behavior is persistence-provider specific.

I'd suggest looking at the presentation from last year's JavaOne called "Persistence In the

Web Tier"

http://developers.sun.com/learning/javaoneonline/2006/webtier/TS-1887.pdf

ksaksa at 2007-7-11 22:47:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

> Regarding caching, what exactly are you referring to

> by "query cache"? Are you saying you

> plan to execute the same query multiple times but

> you'd like the underlying persistence manager

> to avoid trips to the actual database?Whether the

> query is executed by an actual database

> access or is fulfilled through some JVM-local cache

> is not controlled by the spec. Most implementations

> do allow for such caching but the behavior is

> persistence-provider specific.

Yes. I am actually using hibernate behind the scenes as my persistence framework.

> I'd suggest looking at the presentation from last

> year's JavaOne called "Persistence In the

> Web Tier"

>

> http://developers.sun.com/learning/javaoneonline/2006/

> webtier/TS-1887.pdf

I am going to have a look at that.

Thanks again.

Julien.

balteoa at 2007-7-11 22:47:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...