简体   繁体   中英

Using prepared statements with BoneCP

I am using BoneCP with MySQL in Java.

I have several threads, and overtime they will be requesting a connection from the pool to perform some query. The queries are repetitive so I'd like to use prepared statements.

I've used prepared statements before, when I only worked with one connection (and no pooling). But now that I have pooling, I'm not sure how's it done:

As far as I am concerned, a prepared statement is linked to a particular Connection instance. If you return the connection to the pool, then the prepared statement is (or should be) destroyed. My threads only request a connection when they need to do a query - they return it after it is done so they don't retain any connection for a long time.

How do I use prepared statements along with connection pooling?

I personally recommend disabling pool-level PreparedStatement caching, and enabling it at the driver-level. You can read here how to do it.

Nothing is done for you. The right way to do it is to clean up all you SQL resources as soon as you're done with them. You should use them in the narrowest method scope possible.

The JDBC driver may cache the PreparedStatements , but that's not a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM