title: How to handle database connection fail with Hibernate, C3P0 on Java EE?
body: <p>I am working on Java EE JSF application using Hibernate with C3P0 connection pool. I have tried to search anything possible and impossible and tried many things, but couldnt figure this out.</p>
  
  <p>The problem is handling database connection fail, for example when database is shut down. I couldn't find a way how to catch ecfeption which I could use to show error status on user interface. The only thing I could do is see some exceptions in console:</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
      at sun.reflect.GeneratedConstructorAccessor408.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.Util.getInstance(Util.java:386)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
      at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2404)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2325)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:834)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:46)
      at sun.reflect.GeneratedConstructorAccessor407.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
      at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)
      at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195)
      at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184)
      at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
      at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086)
      at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073)
      at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
      at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810)
      at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at sun.reflect.GeneratedConstructorAccessor404.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:358)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2498)
      at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2343)
      ... 18 more
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:579)
      at java.net.Socket.connect(Socket.java:528)
      at java.net.Socket.&lt;init&gt;(Socket.java:425)
      at java.net.Socket.&lt;init&gt;(Socket.java:241)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:308)
      ... 20 more
  ]]
  </code></pre>
  
  <p>But I can't catch this, it's just on console and it's not even printed in web browser response (client thread does not receive any exception, it's just hanging while trying to load the page for infinity).</p>
  
  <p>Here is my C3P0 configuration:</p>
  
  <pre><code>c3p0.testConnectionOnCheckout=true
  c3p0.idleConnectionTestPeriod=60
  c3p0.acquireIncrement=1
  c3p0.preferredTestQuery=SELECT 1
  c3p0.acquireRetryAttempts=1
  </code></pre>
  
  <p>My question is: <strong>How to handle case of database connection fail in the user-friendly way?</strong></p>