Search code examples
javadatabasecachingcache-control

How to reload classes storing database information at frequent intervals?


We have a WebApplication which at start-up fetches all required information from Database and stores it in Java maps.

These maps are later used throughout in the application to fetch information required.

But in this approach whatever updates are done in database are not reflected till you restart the application.

So how can we automatically reload such classes / repositories in Java?

Thanks in advance.


Solution

  • What I understood is that that you need to reload the classes only because you need to refresh the data, which was loaded from the DB during the start-up. Isn't that true? If yes, then if some other mechanism can get your data refreshed, then I don't think you would still need to reload your classes.

    Try using some caching mechanism. Take a look at EhCache.

    You can go with Quartz scheduler, as others pointed out. But wait... that's also another library.

    Well, you can also go by yourself using Timer and TimerTask, otherwise. Just schedule that task for repeated execution, as you pointed out somewhere in the comments that you need to load it every hour.