Subject Can you use PreparedStatements together with connection pooling?
Author phil_hhn
Hi, in our Java app we have some general-purpose helper classes which hold on to some regularly-used PreparedStatements. This all works well.

However we have a java-based web app in which we use connection pooling. What happens to these PreparedStatements in this scenario? Are PreparedStatements 'bound' in any way to the connection that created them? I.e after the PreparedStatement is created, can it be used as its own entity, completely independent of the connection on which it was created? If you close the original connection it'll break the PreparedStatement, won't it?

E.g we create PreparedStatement1 with thread1/connection1, then connection1 gets released back to the pool. If connection1 then gets allocated to thread2 (and that is busy using the connection) then thread3 starts using the PreparedStatement1, is there going to be any problem?

I ask this not because I've experienced any issues yet, but I want to avoid any corruptions (these sorts can be hard to track down) and follow 'best practice'. I've seen what can go wrong if two threads simultaneously try to execute queries on the same connection...

Thanks in advance, Phil