IntelliJ Java IDE
The moose likes Object Relational Mapping and the fly likes Implications of sharing an entity manager in a web application... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Implications of sharing an entity manager in a web application..." Watch "Implications of sharing an entity manager in a web application..." New topic
Author

Implications of sharing an entity manager in a web application...

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 383
Hello,

I would like to put an entity manager instance into the ServletContext/application scope of a web application.

The reason for this is that I want to cache the results of a query (I want to use the query cache) and it seems that I need to put the entity manager either in session or in application scope in order for the query cache to work.

Now I am VERY worried about the thread safety issues or the perfomance issues of having only one entity manager instance for use by several clients.

My questions are therefore as follows:

-What are the implications regarding thread safety of having a single entity manager for use by several clients?
-What are the potential performance issues of having a single entity manager for use by several clients?
-If the solution of having one EM per web app is not feasible, then what other solution do you advise in order to be able to take advantage of the query cache?

Any clue welcome,

Thanks in advance,

Julien.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16622

You cannot allow more than one use use an EntityManager, you will lose concurrency, basically, and eventually, you will get an OutOfMemoryException. The key with query cache, is that it does not cache the results objects. It only caches the IDs of the resultset. And if new records are added to the database, or records removed, then what you have in the query cache is no longer up to date. So you might put some queries in the cache for tables data that never if ever changes. Or just use regular second level cache instead of the query cache.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Yuval Goldstein
Greenhorn

Joined: Dec 27, 2006
Posts: 18
Hey, there is another thing.
I would'nt hurry to mix presentation tier code (servlets, jsps, viewhandlers) with the entity-manager.
You can create and abstraction and decouple "data fetching" and "showing data".

Cheers.
 
IntelliJ Java IDE
 
subject: Implications of sharing an entity manager in a web application...
 
Threads others viewed
NX:Client crashed cause deadlock in LockManager
how make an object available for entire cluster?
long post IBM.158
Number of EJB Home Instances
Relationship among Data class, RAF instance(s), and clients
IntelliJ Java IDE