Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sam White
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • 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
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • 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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic