Search code examples
javavalidationconnectionc3p0

Will C3P0 ComboPooledDataSource getConnection() always return valid Connection?


What are possible return values from that method?

  1. null?
  2. invalid connections?

The question is should I check if the returned connection isn't null or is valid? Or should I just catch the SQLException? Is the returned connection always valid if no SQLException is thrown?


Solution

  • From my experience (and from javadoc of DataSource) this method will return you a connected Connection, you can use to query your database. If pool is exhausted, method will block until a Connection becomes available.

    In the unlikely event of a loss of cabin pressure database connectivity, these errors will happen all over you application. If you want to check your connections at checkout / at regular intervals - c3p0 has configuration options that do that for you.

    See http://www.mchange.com/projects/c3p0/#testConnectionOnCheckout for configuration options.