Automatic reconnect to postgres with Grails after restarting postgres - postgresql

Our web application is running on postgres 9.3 and Grails 2.5.3. When we restart postgres (/etc/init.d/postgresql restart) and access the website the exception "This connection has been closed." until we restart the grails app.
However, we would like the grails app to automatically reestablish a connection with the db, after we have restarted postgres. So we would like the app to automatically reconnect to the db by itself, without us having to restart the grails app.
Is that possible and if so how?

Check out this answer: https://stackoverflow.com/a/32437524/2166188 and add to config:
testOnBorrow=true
validationQuery=SELECT 1

Related

Not Connected to Server Postgres database , showing timeout expired

I am currently working on Spring Boot project which is connected to Postgres database. I am using PG Admin tool to connected to database. While I am running my project, I am getting exception and that is related to database.
The password I am giving is correct, still it is not connecting to the database. I am not using any AWS services.
How I can resolve this issues related to timeout expired?

connect grails 3 app on heroku with postgresql

I have deployed grails 3 app on heroku and setup a database addon postgresql. However I don't know the process to connect them. please show me the stepwise process to do so

OpenERP - connections to database

I am running OpenERP v6, Windows, PostgreSQL 9.1. When I connect to the database from my Windows OpenERP client, the OpenERP service makes a connection to database. This connection is visible in pgAdmin Server Status window. When I close OpenERP client, the connection is still there. Moreover, if I open OpenERP Client again, then login, the OpenERP service creates another connection to database, visible in Server Status window. Should there be only one connection to database made by OpenERP service for multiple OpenERP Clients? Can someone clarify to me the logic behind the OpenERP connectivity to database? Can this be a bug in the server code? If yes, can you direct to web site where the bug is fixed or to the file name where the relevant code is?
Thanks!
You can run multiple Openerp Server With PostgresSQL 9.1 with changing the port number
You can change the port number by following way:
./openerp-server --xmlrpc-port=8070 --db-filter=your_database_name
You can also manually kill the process which is running on current port number

Is there any way to restart postgres on Heroku?

I deploy a Rails app using Unicorn. After every deployment and after every tweak I do to the DB_POOL I see postgres still hold some connections as idle and new changes are very inconsistently making me wondering If is restarting at all the service after every pool change.
I haven't found any documentation regarding this. Is there any similar command to pg_ctl on Heroku?
No, you cannot restart your Postgres database on Heroku. If you have lingering connections, it's likely an app issue. Try in stalling the pg-extras plugin and looking for IDLE connections:
https://github.com/heroku/heroku-pg-extras
Also, you can try setting up a custom ActiveRecord connection in your after_fork block and enabling the connection reaper, which should clean up any lingering dead connections it finds:
https://devcenter.heroku.com/articles/concurrency-and-database-connections

c3p0 pool cannot establish a connection. How to debug this?

I have a Tomcat and PostgreSQL installed on a server. I'm having a connection problem trying to connect from my servlet to PostgreSQL database using c3p0 pool.
I can reach DB if I'm running Tomcat locally on my laptop. Also I can connect from server to DB using psql (i.e. command line sql utility). But when I'm trying to deploy my servlet to server and establish a connection I'm getting the following error:
java.sql.SQLException: Connections could not be acquired from the underlying database!
com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
...
com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
What should I check to locate a problem? It should be a trivial issue but may be due to 4 a.m. I'm missing something :) Thanks in advance!
PS: Connection from all network interfaces are allowed to database. PostgreSQL JDBC driver and c3p0 pool are distributed in WAR. Tomcat configuration is very default. JNDI is not used.
You need to check a few things:
java.policy which tomcat is using
(e.g.
/etc/tomcat5.5/policy.d/02debian.policy)
db server settings (e.g.
/etc/postgresql/pg_hba.conf)
try connecting without pool first as
in my case c3p0 was hiding important information from me
Adding to #Alexey's answer, I had this issue with Tomcat and PostgreSQL 9.4. In my case, the md5 authentication method in postgres was causing the issue.
If you are using Windows server or RHEL server, make sure you update the authentication method in pg_hba.conf file. Modify it to trust and restart postgresql.