• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to pre-load database on application start-up.

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, with spring mvc 3.0.5, jboss 7.1.1 and hibernate, I am wondering how to pre-load eg. the first 10 records when my application is start-up (deployed)?

At the moment, whenever I execute a productService.findAll() for example, it always wait for a long time (10 secs) for each a page to reload.
Is there anyway "cache" the first 10 returned records somewhere in memory when the application deployed and started up?
Thanks
Sam
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on how your application is structured, you can make some class/object in your application an "InitializingBean" and in its "afterPropertiesSet" method you can run the query and store the first ten results in some local variable. That method is ran right after Spring initializes your bean and injects its properties, so well before your application is totally up and ready to process requests.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or if you want a cleaner way, without coupling your code to Spring.

You can annotate a method in your bean with @PostConstruct and after that bean gets instantiated Spring will call that method.

It is just an alternative to implementing InitializingBean.

in xml there is init-method attribute in the <bean> tag.

So there are three ways. The implementing InitializingBean is the most coupling to Spring.

xml means no annotations in your class, so you could say this is the cleanest. But I prefer @PostConstruct myself.

Mark
 
A wop bop a lu bop a womp bam boom! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic