aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes No shutdown hook? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "No shutdown hook?" Watch "No shutdown hook?" New topic
Author

No shutdown hook?

Bably Das
Greenhorn

Joined: Sep 17, 2009
Posts: 28
Hello,
Is it possible to use a cache for the assignment and not to implement a shutdown hook. My approach is-
I have a DBFileAccess class with two method to load the database records into a cache and a persist database method to load the cache into the database file. For this, in my load Database method -


For persist Cache to Database file


My Data class is Singleton, with all methods synchronized and at application startup, in Data class private constructor, I call the loadDbRecords from the DBFileAccess class.
After each update, delete or create operation I persist database records back to the file instead of persisting records at the time of server shutdown. My server shutdown is simply System.exit(0) as my RandomAccessFile is closed after each read and write. Is this a valid approach to pass the exam? I know there will be slow performance issues but as it is not a criteria in this exam, can I overlook it? Any advise in this direction highly appreciated.

SCJP 1.5, SCJD (URLyBird 1.3.2)
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5278
    
  13

That's a valid approach. Not the most performant one, but that's not a must requirement. The only question I can think of: why using a cache if you write each time to the database file?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Bably Das
Greenhorn

Joined: Sep 17, 2009
Posts: 28
As always thanks a lot Roel for your quick reply.
The only question I can think of: why using a cache if you write each time to the database file?


Being a junior Java programmer myself I find working on a cache easier than the actual file itself.

Thanks,
Bably Das
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

This is one of the ideas I've thought about. The rationale as I saw it is that you only take the performance hit on data writes. Data reads are likely to be much more common, so you really want those to be faster. So, you load the entire database into the cache at startup, and then write changes to both the cache and data file. But you never read from the file after the initial load.

I still haven't decided for certain yet (though I'm leaning towards the "write on shutdown" approach).
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5278
    
  13

Hi Matthew,

Great argumentation

Just a remark: in a (room) booking system I guess there will be significant more updates than when your database file contains customers (with name, address, phone numbers,...). But of course the number of reads would still outshine the number of writes.

Kind regards,
Roel
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Roel De Nijs wrote:Hi Matthew,

Great argumentation

Thanks! .

For completeness - the advantage (as I see it) of the continuous writing is that you don't lose all your data changes in the event of a crash. I also considered a compromise approach (periodic asynchronous writing of changes), but ruled that out as too complex for this exercise (I'd be tempted to do that or some other sort of autosave feature in a 'real' project though).

Whatever I decide, all this is going in my choices.txt file!
 
Don't get me started about those stupid light bulbs.
 
subject: No shutdown hook?