• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
That is good to hear.
reply
    Bookmark Topic Watch Topic
  • New Topic