The apparent deadlock message issued by c3p0, a connection pool manager of Hibernate, can sometimes be misleading.
Most of the times, it is caused by connection errors.
So, check your firewall, and, for Postgresql try the following command from the command line
1 |
psql -h host -U user db |
If this works, you should have no problem connecting to Postgresql.
The apparent deadlock error
In my case I had the following error shown in the console under Windows, Java 8, when starting Tomcat under Eclipse Oxygen
1 |
2017/11/27 18:21:03 WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@15cc07ce -- <strong>APPARENT DEADLOCK!!!</strong> Creating emergency threads for unassigned pending tasks! |
and I was getting the following exception
1 2 3 4 5 6 7 8 9 10 11 |
2017/11/27 18:21:14 WARN com.mchange.v2.resourcepool.BasicResourcePool - com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1e723184 -- <strong>Acquisition Attempt Failed!!!</strong> Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: org.postgresql.util.PSQLException: FATAL: <strong>the remaining connection slots are reserved to non replica superusers connections</strong> at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:469) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:112) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125) at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30) at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22) at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30) at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24) at org.postgresql.Driver.makeConnection(Driver.java:393) |
In the postgres log I was seeing many authorized connections opened by c3p0.
I also used Wireshark and RawCap to capture the connections to the db and it seemed they were acquired, as the log said, but after seeing Postgres saying “ready for queries”, they were closed. I used this tutorial to understand how to use Wireshark with RawCap under Windows 10.
The solution
After some investigation I discovered I was using the wrong JDBC postgresql driver!
After changing to PostgreSQL JDBC 4.2 Driver, 42.1.4 which is listed as a driver for Java 8, all went fine.