• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Catching Ctrl C

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a Java GUI Application which has a Db connection.
Now when the application window is closed using the normal 'X' button on the top right corner,I can catch the WindowClosing event and close the db connection.
But since the application is running from a dos prompt,I can even close it using a Ctrl C.
Is there any way for me to catch the Ctrl C and the take appropriate action like closing the open db connections?
Thanks in advance,
Chinmay.......
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Runtime.addShutdownHook(Thread hook)
This won't keep the program from terminating, but lets you do some cleanup while it does.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what happens if someone pulls the plug on the client computer. How do you clean up then?
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't clean up. But you could put some start up code that would try to fix what was not properly released during the last running of the application. This could be rebuilding logs or reporting on last state of the application.
However, outside of using a UPS, there is no such thing as a "Loss of Power Exception or Event"
 
Nilesh Pereira
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My point exactly. Which is why I don't think it's a good practice to trap Ctrl-C. What if the user does an "end task" in Windows, or "kill -9" in UNIX... or the network dies... or a hard disk crashes. There are just too many possibilities. It makes better sense to do damage control either on the server side, or while starting up as Rodney ponted out.
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Point accepted Nilesh!
But when u r working on a single db connection,u have to try and plug as many ambuities as u can.
Thanks,
Chinmay
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic