What is the best solution when dealing with user settings

We have a webapp, when a user logs in, all the user en application settings are read (only once) from the database and stored in a session bean (for quick access).

But when some application setting is changed, I want to tell all the session beans that they need to reload the settings.

Is there a good solution for this?

Thanks in advance.

[364 byte] By [marcbiera] at [2007-10-2 23:50:30]
# 1

There are several ways to do this:

1) Put a timestamp on the application settings tables whenever they are changed. At the beginning of each request, send a small query just for this timestamp and compare it to a timestamp for when the session was created. If it is newer then the session then force a full refresh of the session data, otherwise keep using pre-loaded session data. This keeps normal operation quick since the SQL needed for the intial test is simple and quick, but will get the user the new information they need when they need it.

2) Collect your sessions into an external collection stored inside application scope. Have a trigger that when the application settings change, the session objects are iterated through and updated. This way the settings may be updated without the user making a request - and the updates may be time-invisible to the user. But you could get into situations where you are updating settings at the same time you are reading settings, so you will need to synchronize your access.

3) Don't put application settings in the session. The session should be for user settings only. The application scope (Servlet Context) should be used to store the application settings. As such, when application level changes to the DB are made, the application-level object is reloaded. You only have one copy in memory, it updates just once, no need to iterate through sessions, and users who use the application settings are using the most recent copy.

stevejlukea at 2007-7-14 16:35:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for your reply!

Last question:

How can I iterate thru all session beans (of a certain name)?

Is this possible to do with the application object (in a jsp)

Something like.....

Iterator it = application.XXX();

while (it.next()) {

MySessionBean b = (MySessionBean)session.getAttribute("mysessionbean");

................

..........

}

marcbiera at 2007-7-14 16:35:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> Thanks for your reply!

>

> Last question:

> How can I iterate thru all session beans (of a

> certain name)?

>

> Is this possible to do with the application object

> (in a jsp)

>

er...session beans can be had from the session object. Please read Steve's reply carefully -

<quote>

The session should be for user settings only. The application scope (Servlet Context) should be used to store the application settings.

</quote>

The context object is common (aka shared) between all current users of the application.

If you want to get a handle to all objets bound to a session,

Enumeration e = session.getAttributeNames();

while (e.hasMoreElements()){

Object attributeName = e.nextElement();

Object attribute = session.getAttribute(attributeName);

}

If you want to get a handle to all objets bound to the context as context attributes,

Enumeration e = getServletContext().getAttributeNames();

while (e.hasMoreElements()){

Object attributeName = e.nextElement();

Object attribute = getServletContext().getAttribute(attributeName);

}

ram.

Madathil_Prasada at 2007-7-14 16:35:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...