• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sharing member variables in EntityBean instances?  RSS feed

 
mik laan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Are static member variables shared among every instance of same EntityBean?
Is it against "good programming style" to use static member variables as a "data-cache" in CMP entitybeans?
Br
ML
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are specifically prohibited by the EJB spec from using static member variables in an EJB (unless they are declared final and are thus read-only).
Kyle
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, on the other hand, it is OK to use static member variables in ANOTHER class from an EJB, but again -- only as read-only caches. It's a really bad idea to try to use static variables as a read/write data cache.
Kyle
 
mik laan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okey!
So, what could be the best way to cache read-write data with EntityBeans (or other way to resolve my problem)?
The whole picture that I have in my mind is following:
1. EntityBean reads data from DataBase to e.g. a collection class (which works as a cache).
2. A Singleton class is created and is working as an interface to WebApplication (client).
3. WebApplications can read and update data that is hold in EntityBean's collection class using Singleton class.
4. Web Container is clustered, so there will be as many singleton classes as JVMs.
So, the need is to have consistent data in EntityBean's collection class.
If there is a better approach to do this, please let me know.
Thanks,
ML
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on your application server. Different servers offer different caching functions. There's no single solution that works best across all servers.
Kyle
 
mik laan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okey, my environment is WAS 4.0.x.
Br
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that you are in WAS 4.0 I would recommend NOT caching Entity data in a clustered environment. Instead take advantage of WAS 4.0's HttpSession caching to do read/write data caching. WAS 4.0 does not include a clustered solution for Entity bean caching.
Capabilities for clustered caching will come in later versions of WAS (no, not 5.0 either) and there are other options you can follow to do clustered caching (see http://www.messagingpatterns.com/ for the Distributed Cache updated pattern) but it's just easier in 99% of cases to cache at the HttpSession level.
Kyle
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!