• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there anything wrong with adding a ShutdownHook to the Data class constructor?

 
Mxolisi Veco
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day all.

I was thinking of adding Runtime.getRuntime().addShutdownHook to the Data class contructor so that I can place a thread in there that will close the database file just before the program terminates.
Since there is no method that that says connectDatabase on the Interface that I was given, I assumed that there will be no need for a method that says closeDatabase(). So I will add some code to the Data class constructor to close the database instead of extending the interface I was provided with to add a method that closes the database.

Is there anything wrong with this 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 Mxolisi,

(That's not an easy first name to write and pronounce )

I did exactly the same, although I added a start and stop method to my custom interface. Because of my approach (record cache) a nice cleanup and stop of the database is very important, so I added Runtime.getRuntime().addShutdownHook() to the constructor of the Data class. That gives the guarantee that the database is closed nicely and everything is written back to file.

Kind regards,
Roel
 
Mxolisi Veco
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
That is good to hear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic