Re: Is it thread-safe to pool the PreparedStatement?

Horaci Macias <[email protected]>
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <02B7F7F404E66344814C4D5852E401A30BB539@caotta0025m.na.ubiquity.net>
I don't think PreparedStatements can be used by multiple threads
concurrently since you could have thread A calling setString(1, "a") and
thread B calling setString(1, "b") on the same prepared statement and
the result won't be what you expected. Or in other words, regardless of
them being thread safe or not, you (your app) probably don't want to use
them concurrently anyway.

It's worth pooling them, specially if the app makes lots of db
operations, and in fact jdbc 3.0 already does that. See
http://www.javaperformancetuning.com/tips/jdbc_prepared.shtml for
details.

In your case you can make sure only 1 thread can use a particular
prepared statement when the thread tries to retrieve it from the map. If
the map is the only place you can retrieve preparedstatements and access
to the map is properly synchronized, then you don't have to check any
uses of preparedstatement on the rest of the code.

You can still reuse preparedstatements later, just not at the same time
by multiple threads, unless you want to have some kind of random
behaviour.

Hth,

Horaci

-----Original Message-----
From: Discussion of advanced Java topics.
[mailto:[email protected]] On Behalf Of Carfield Yim
Sent: 20 August 2007 12:47
To: [email protected]
Subject: [ADVANCED-JAVA] Is it thread-safe to pool the
PreparedStatement?

I just join a project, this existing team member of this project
concern performance a lot so their pool all PreparedStatements into a
map (probably not a nice optimization, but I have no say about that)
when the application start. And then reuse those preparedstatement
again and again.

I haven't get time to check all method call using those
preparedstatement are synchronized properly. If that is not the case,
is it thread-safe to pool the PreparedStatement?

===================================
This list is hosted by DevelopMentor  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com



Information contained in this e-mail and any attachments are intended for the use of the addressee only, and may contain confidential information of Ubiquity Software Corporation. All unauthorized use, disclosure or distribution is strictly prohibited.  If you are not the addressee, please notify the sender immediately and destroy all copies of this email. Ubiquity Software Corporation plc, a company registered under the laws of England and Wales, Registration 2719723, registered offices at Eastern Business Park, Building 3, Wern Fawr Lane, St. Mellons, Cardiff CF3 5EA, Wales, United Kingdom.

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.