This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Julien Martin
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yuval Goldstein
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!