• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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 ?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic