• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrency with Application scope (Web App) objects

 
Pranav Sharma
Ranch Hand
Posts: 258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a situation where all users need (only read, no write) to have access to the same information.

My solution is to add the needed info to a bean and stuff it into the application scope, this way I don't need to stuff the bean into each request or session. However I also need to refresh this information at regular intervals, note I will be completely replacing the object. I'm worried that I may step on the object just when a user is trying to access this object, ie., it is not synchronized.

I'm currently using a simple POJO to display the information on a JSP via struts.

Is my initial solution correct for the situation and if so how would I take care of the potential concurrency issues.

TIA
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might consider using a ReentrantReadWriteLock for the code blocks that read/write that object. It lets any number of read accesses go through, but a single write access will block until it can run exclusively.

If the code blocks are short (and thus quickly done executing) you could also synchronize them completely. Uncontended lock acquisition is not nearly as costly as it used to be in early JVM implementations.
 
Alan Mehio
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us say there is a scheduled task which get holds of the application context and replace the saved bean.
Instead of application context, you can have a static cache which can be accessed by many user at the same time. Use ConcurrentHashMap
which offers a higher degree of concurrency than Hashtable or synchronizedMap
it is very optimized for concurrecy access. Save the bean inside it as key=value; then the schedluer just invoke the put(predefinedKey, bean);

If the search end up with no value, then it then synchronizes on the appropriate bucket lock ( up 32 locks ) and searches the chain again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!