On Wed, 29 Nov 2000 09:44:04 -0500, Ruoqi Chen <[EMAIL PROTECTED]>
wrote:

>Rickard,
>
>May you tell me how to synchronize the static hashtable of a bean?  Is
>synchronization allowed in beans?

First, yes it is allowed since this is static read-only state. I don't
want to pick up the discussion again on what is allowed and what is not
allowed. This Will Work.

Here's what you should do. In your stateless session bean have a static
field with a hashtable:
static Hashtable valueCache = new Hashtable();

in your session bean code you should use it as follows:
public Collection getValues(String key)
{
  Collection values = valueCache.get(key);
  if (values == null)
  {
    // Load values from DB
    values = ...
    valueCache.put(key, values);
  }

  return values
}

Since java.util.Hashtable is synchronized there is no chance for two
threads to update it simultaneously.

The above pattern will allow you to put the list in a database, but
cache it so that the DB is not used all the time for this static data.

If there is a chance that the value list is updated during the
application lifetime (i.e. it is semi-static), then just add a timeout
that clears the cache at regular intervals. It will be reloaded the next
time it is used.

regards,
  Rickard

--
Rickard �berg

Email: [EMAIL PROTECTED]

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to