Java – Using Prepared Statements and Connection Pooling together in MySQL

connection-poolingjavaMySQLprepared-statement

Presently, for each query, a Prepared Statement is created and reused. I am not using any connection pool. C3P0 is a widely recommended library for the same.

But, as a PreparedStatement is tied to a connection. In the pooled environment, the connections are returned to the pool, effectively making PreparedStatement unusable. Am I correct about this conclusion? Is there any way to use Prepared Statement and connection pooling together?

Update: This is stand-alone application. So, I cannot use a framework just to get connection pooling right.

Best Answer

It depends on the pooling mechanism you use. Most Java EE appservers have connection pool implementations where there is a prepared statement cache alongwith each connection in the pool. So the prepared statements are reused as well as the connections. I am not aware of any standalone pooling mechanisms though which have this functionality.