• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that a yes/no question? No.

But you only ever want one Factory for your entire application. Factories are heavy-weight objects that can take time to build.

You won't have to put it in the ServletContext though. The best practice is to make a HibernateUtil class that is a singleton for the Factory class.

Mark
 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Mark,

You only partially replied to my question though.

I am still worried that using multiple instances of EntityManagers will prevent the query cache from working properly i.e. the query cache will be recreated by each of the different instances of EntityManagers.

This is a yes/no question:
CAN I HAVE DIFFERENT INSTANCES OF ENTITYMANAGER OPERATING ON THE SAME QUERY CACHE???

Thanks in advance,

Julien.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Julien Martin:
Thanks for the reply Mark,

You only partially replied to my question though.

I am still worried that using multiple instances of EntityManagers will prevent the query cache from working properly i.e. the query cache will be recreated by each of the different instances of EntityManagers.

This is a yes/no question:
CAN I HAVE DIFFERENT INSTANCES OF ENTITYMANAGER OPERATING ON THE SAME QUERY CACHE???

Thanks in advance,

Julien.


Depends on what you define as a query cache. In Hibernate's second level query cache it is not based on entitymanager but on the Factory as a whole, so no problem there. If you use JDBCs caching of queries, then I suggest not to use it at all, because it really isn't going to give you a boost of performance. There are much better places to get better performance.

Also, please do not yell, that is not polite, and goes against the JavaRanch Be Nice policy. The Bold part was ok.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic