hi all: I have 2 question, and I really appreciate any suggestion from experieced developers. 1- If I have a certain set of information in the database, which I need it almost in every page I display (for example: a list of the functions available to a certain user) is it adeqaute to bring all this data and store it in the serverlet context object, so that it will be availabel to all other servlets? If I use this approch, if the information in the database changed, how can I change it in the application servlet context without rebooting the server? Note: consider a clustered application server. 2- If the first approche is not adequate, how can I approche the this problem? How can I make infomation available to every page and servlet without quering the database every time a user request a page? Not: The size of information is large, and it is confidinial, so I can't store it in a flat file. thank you all
SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
If the application is distributed, ie the <distributable/> tag is set in web.xml, then you may have several VMs running the application and neither the servlet context nor events of servlet context are propagated when the container decides to create a new VM. In that case the Servlet Spec says that you should use the session object, a database or EJBs. If you have huge amounts of data as you indicate, storing in each session object would probably be a bad idea. My instincts on this one is to use a stateless session EJB using a local interface and BMP. You would make the JDBC calls in the session bean and cache the data there upon creation. No entity beans would be involved and since you are using a local instead of remote interface performance would improve greatly. Now this assumes that the database doesn't change every few seconds or even minutes. You could then refresh the session bean data cache at a predetemined interval. There may be instances when the bean's data cache is stale but the only alternative is to hit the database every time. This belongs in the Servlet forum, so I am moving it there. [ April 03, 2004: Message edited by: Michael Morris ]
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Hanna, Is the data specific to each user or is it for everyone as a whole? If the data is used by everyone, you can store it in the application scope. (singleton or static variables) Similarly if the user has some sort of "type", you can store the large data in one place and use a function call to get what is needed for the specific user. The point of this is to minimize the amount of shared data in the session. If the data really is unique per user, do what Michael said.
hi Jeanne : The data is shared among many users, however every user group has a distenct set of data. If I store it in the application scope, how can I update it when the database changes without redeploying the application? also, consider a clusterd application server. I created a servlet that go to the database and bring the new data, however, it doesn't work in a clustered application server. Only one VM get the new data, the others don't know anything about it. As was suggested here, EJB will be one solution, but I don't have experiece with EJB nor any one on my team.
Hanna, Caching could help you out here. You could refresh the cache every set time interval. Or you could have a database table with a "last updated" time and refresh the cache if the time is greater than the most recent time the cache was loaded.