Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

No shutdown hook?

 
Bably Das
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bably Das
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic