• 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

How to close Hibernate C3P0 Connections properly?

 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, simple question. How do I close the connection hibernate/c3p0 opened?

At the moment c3p0 continues to open new connections even if i close the SessionFactory.
I want to close all connections before shutting down the application. Just exiting is not a valid option imho.
 
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of graceful shutdown the connection pool will close all open connections.
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does "graceful shutdown" mean in this case. I am to program a fat client which has two database connections. One the fat client is connected to most of the time and one to update the database from a main server.
Problem is now that I must not shutdown the program when i log out of any of those connections.

The strange thing is that sometimes it seems to work and other times it is not... all the times I try to close them by closing the sessionFactory.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Manuel,

were you able to find a solution for that problem? Im running into the same situation, I need to be able to close all connections to the database while keeping the application running. I know that the pool manages that. In my case spring instantiates com.mchange.v2.c3p0.ComboPooledDataSource.

Do you have any pointer to the solution?

thank you very much
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to manage that by loading the two factories into a map like

Actually i wrapped the sessionfactory into a new class to delegate its methods to my needs.
If i want to close one now i need to get it from the map, call the close method and remove it from the map.
You need to make sure that no other hard reference is pointing to this sessionfactory for it to work properly.

Might not work for you.
 
Luis Rosa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the reply,

I am solving this problem in another way, which is off this topic but might help someone looking for this answer like me.
So I wanted to close all connections used by c3p0.ComboPooledDataSource and stop it from doing more connections so that I could be able to drop and restore a database, all this in production server without disturbing other applications or databases.
the best approach that I found so far, and seems even more reliable, is to do all this work just on the database server , in this case postgres
in a nutshell :
- stop database from having newer connections (only to this database) UPDATE pg_database SET datallowconn = FALSE WHERE datname = 'database';
- get list of existing connections psql -U swoffice -t -c "select procpid from pg_stat_activity where datname='swoffice' and current_query not like 'select procpid from pg_stat_activity%';"

- kill connections (might cause data corruption, so backup first)
for(int procpid : procpidList)
psql -U swoffice -t -c "select pg_terminate_backend(procpid) from pg_stat_activity where datname='database' "

now free of connections to drop and create db using pg_dump and pg_restore
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luis Rosa wrote:thank you for the reply,

I am solving this problem in another way, which is off this topic but might help someone looking for this answer like me.
So I wanted to close all connections used by c3p0.ComboPooledDataSource and stop it from doing more connections so that I could be able to drop and restore a database, all this in production server without disturbing other applications or databases.
the best approach that I found so far, and seems even more reliable, is to do all this work just on the database server , in this case postgres
in a nutshell :
- stop database from having newer connections (only to this database) UPDATE pg_database SET datallowconn = FALSE WHERE datname = 'database';
- get list of existing connections psql -U swoffice -t -c "select procpid from pg_stat_activity where datname='swoffice' and current_query not like 'select procpid from pg_stat_activity%';"

- kill connections (might cause data corruption, so backup first)
for(int procpid : procpidList)
psql -U swoffice -t -c "select pg_terminate_backend(procpid) from pg_stat_activity where datname='database' "

now free of connections to drop and create db using pg_dump and pg_restore



What you are talking about is after the application shut down or got hang and nothing can be done. But the OP seems to be more concerned about controlling it form the application itself which might be the case for platform/framework development.
 
Luis Rosa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe I was not clear, but no I will have the application running all the time, actually the code will run in the application in tomcat. my initial idea was to control the connections from the application, but decided to do the work directly in the database without caring for the exceptions occurring in the application.
it works well this way
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luis Rosa wrote:maybe I was not clear, but no I will have the application running all the time,...


How it runs when you stop DB acquiring any connections and delete all the existing ones, when the application need to interact with the DB?
reply
    Bookmark Topic Watch Topic
  • New Topic