How do I clean up an application when sending an interrupt (ctrl+c) when using the application plugin?

I am running an application on the command line with the gradle application plugin and the ‘run’ task. It connects to a database and does transactions using a JPA EntityManagerFactory and EntityManagers. One issue I am seeing is that quite often when I ctrl+c the application and it is mid-transaction, it leaves behind the connection in postgres. I tried adding a shutdown hook to the application to close the entityManagerFactory, but that does not seem to help, it seems as if the shutdown hook is not running.

Any advice?

Have you found answer to this?

I never did find a solution. A coworker noticed it seemed to only happen when running from gradle within Cygwin. So I just avoid Cygwin these days.