jQuery in Action, 2nd edition*
The moose likes Object Relational Mapping and the fly likes How to close Hibernate C3P0 Connections properly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to close Hibernate C3P0 Connections properly?" Watch "How to close Hibernate C3P0 Connections properly?" New topic
Author

How to close Hibernate C3P0 Connections properly?

Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

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.


Please correct my English.
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

In case of graceful shutdown the connection pool will close all open connections.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

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.
Luis Rosa
Greenhorn

Joined: Feb 15, 2012
Posts: 3
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

Joined: Jul 19, 2011
Posts: 175

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

Joined: Feb 15, 2012
Posts: 3
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
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Luis Rosa
Greenhorn

Joined: Feb 15, 2012
Posts: 3
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

Joined: Mar 24, 2008
Posts: 3817

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to close Hibernate C3P0 Connections properly?
 
Similar Threads
Hibernate Session Problem
how to configure persistence.xml - java persistence api - with apache commons pool
How to detect wrong login earlier
Hibernate Connection Pooling.
How to configure c3p0 connection pool weblogic ??