Cris,

On 4/8/21 07:54, Berneburg, Cris J. - US wrote:
Hi Folks

I'm working on an old legacy app and noticed something.  It caches a bunch of info 
(lookup table data) from the database using a ServletContextListener.  I think opening DB 
connections in a listener is reasonable.  While there is no business logic in the 
listener, I'm not sure doing a bunch of DB heavy-lifting operations in a context listener 
is a "good thing", although I don't really have a concrete reason why.  Perhaps 
I'm just being fussy.

Anyway, in your opinion:

   1.  Is performing DB heavy-lifting operations in ServletContextListener a 
"reasonable" practice?
   2.  Is there a "better" way of caching said items at application startup?

Thanks for your time and consideration.  :-)

IMHO there is no better way than using a ServletContextListener to load things at startup. Your only other spec-compliant option is to use a Servlet with load-on-startup set and do your work in the init() method, which is ... ugly.

SCL is the way to go, here.

Another option would be to perform "lazy loading" instead of a-priori loading of this data. You will take the hit of loading the data when it is first requested, which may negatively impact user experience. It might also mean that you have to be more careful about cross-thread synchronization, etc. since you can't guarantee that the work has already been done before a client tried to access the cache.

If you are concerned about startup times, lazy-loading is a good solution. It can also improve your memory usage if that data is never actually needed.

We have a primary application at $work where we need to have a lot of information in mrmoey to be able to do important stuff. This information evolves over time and has a long (essentially infinite) history. We can never really get rid of anything entirely, but the older some of that data gets, the less likely it is to be used. We loaded 100% of it every time at startup.

At some point, it started taking several seconds to launch the application, then eventually several tens of seconds and I decided it wasn't tolerable anymore.

So I switched to loading things on-demand and it made not only a significant performance improvement on startup (hey! we are loading nothing on startup now! super fast!), it significantly reduced the memory footprint of the in-memory cache of data because that rarely-used older stuff was just never being loaded at all.

To be sure, it was a significant change to our application, but one well worth the investment.

(We also have a user-initiatable process to "reload" the data that was taking the same amount of time. Not it just empties the cache and does nothing else. More faster. :)

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to