It's not a secret anymore!
The moose likes Object Relational Mapping and the fly likes How to pre-load database on application start-up. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to pre-load database on application start-up." Watch "How to pre-load database on application start-up." New topic
Forums: Spring Object Relational Mapping

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

sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 229
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?
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 187
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

Joined: Feb 05, 2001
Posts: 17276

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.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: How to pre-load database on application start-up.
It's not a secret anymore!