问题
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.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/2166460/c3p0-pool-cannot-establish-a-connection-how-to-debug-this