Earlier today I encountered the following stack trace whilst using C3P0 and Hibernate:
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool - An Exception occurred while trying to check a PooledConection into a ResourcePool. com.mchange.v2.resourcepool.ResourcePoolException: ResourcePool [BROKEN!]: Tried to check-in a foreign resource! at com.mchange.v2.resourcepool.BasicResourcePool.checkinResource(BasicResourcePool.java:657) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$ConnectionEventListenerImpl.doCheckinResource(C3P0PooledConnectionPool.java:636) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$ConnectionEventListenerImpl.connectionClosed(C3P0PooledConnectionPool.java:630) at com.mchange.v2.c3p0.util.ConnectionEventSupport.fireConnectionClosed(ConnectionEventSupport.java:55) at com.mchange.v2.c3p0.impl.NewPooledConnection.fireConnectionClosed(NewPooledConnection.java:510) at com.mchange.v2.c3p0.impl.NewPooledConnection.markClosedProxyConnection(NewPooledConnection.java:381) at com.mchange.v2.c3p0.impl.NewProxyConnection.close(NewProxyConnection.java:1246) at org.hibernate.connection.C3P0ConnectionProvider.closeConnection(C3P0ConnectionProvider.java:92) at org.hibernate.jdbc.ConnectionManager.closeConnection(ConnectionManager.java:474) at org.hibernate.jdbc.ConnectionManager.aggressiveRelease(ConnectionManager.java:429) at org.hibernate.jdbc.ConnectionManager.afterTransaction(ConnectionManager.java:316) at org.hibernate.jdbc.JDBCContext.afterNontransactionalQuery(JDBCContext.java:268) at org.hibernate.impl.SessionImpl.afterOperation(SessionImpl.java:444) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1153) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
Apologies for the scroll bars… but hopefully this is indexed neatly by Google to help others!
After a bit of investigation it turned out this was due to a multi-threading issue and the solution can be summarised quite neatly:
never shut down Hibernate before all threads have finished what they’re doing (if they are using Hibernate)
Otherwise, you may get the above exception (or an equally unhelpful exception).