Meaningless Drivel is fun!*
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

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

sam White
Ranch Hand

Joined: Feb 18, 2011
Posts: 208
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

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pre-load database on application start-up.
 
Similar Threads
When does the servlet life start?
Diffrence betweeen Listener and <load on Start Up>
What is the use of this servlet?
Load Properties file when tomcat will start
ServletContextListener