Andreas Maschke wrote:
Hi Dear All,
 
being a newbee to EJB I hope the following questions don't bother you: :-)

I have a static class XYZData with a static Vector containing some data. The Vector acts like a cache. If I request some record from the class,
it first searches the Vector. If there is no matching record it is loaded from the Database and appended to the list. 
Now I have a stateless session bean which imports this class. There is a method which calls the static method of the class XYZData to request
records.

The questions are: 
How many instances of the class XYZData (and the cached data) are created by an Application Server if it creates multiple instances
of the session bean? 
Is this a valid design to implement a (read-only) cache in EJB? Are there better ways? (Using "read-only entity beans" seems to be very
vendor-specific.)


You could certainly do what you're describing. You will get one copy of the static data cache per VM. If you run one app server instance in a single VM, you get one copy. If you run a cluster of app servers, though, each one will have its own cache.

However, this isn't a very common programming paradigm. Keep in mind that your database server is going to cache frequently read data anyway. If you have a nice fast database server and effective connection pooling (so you're not having the overhead of reconnecting), you may find that pulling this frequently-accessed data out of the DB is not a great deal less effective than pulling it out of your cache. Also you may have a problem with your architecture re updates. E.g. if some person or process ever updates your DB tables directly, you will have a stale cache in your server.

Some app servers can automatically maintain a memory cache for you, and can even manage a shared cache that is accessed across multiple server instances in a cluster (e.g. GemStone, Persistence). But this is not a standard feature.
 
 
 

Reply via email to