Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / March 2008

Tip: Looking for answers? Try searching our database.

thread safe JDBC code

Thread view: 
Steve - 24 Mar 2008 00:40 GMT
How to guarantee the JDBC code is thread safe? Should we put
synchronize block in JDBC methods that performs update/delete
operations?

please advice. thanks.
Arne Vajhøj - 24 Mar 2008 01:46 GMT
> How to guarantee the JDBC code is thread safe? Should we put
> synchronize block in JDBC methods that performs update/delete
> operations?

The exact same thread safe rules applies to code using JDBC as
any other code.

If you are worried about the database, then don't think
synchronized - think transactions and transaction isolation
level.

Arne
Arved Sandstrom - 25 Mar 2008 18:56 GMT
> How to guarantee the JDBC code is thread safe? Should we put
> synchronize block in JDBC methods that performs update/delete
> operations?
>
> please advice. thanks.

Here are some articles (despite the titles, not MySQL specific) that discuss
the general problem:

http://www.mysql.com/news-and-events/newsletter/2003-04/a0000000154.html

Key phrases: "The answer to this problem is to realize that a unit of work
is scoped by a java.sql.Connection instance. In fact, even transaction
control is scoped to a java.sql.Connection, via Connection.setAutoCommit(),
Connection.commit() and Connection.rollback(). Therefore, let only one
thread at a time use a Connection instance to do its unit of work. There are
multiple strategies for doing this, from giving each thread its own
Connection instance, or sharing Connection instances via a Connection pool."

Note: one can also use ThreadLocal.

http://dev.mysql.com/tech-resources/articles/connection_pooling_with_connectorj.html

Has code example. Recall that local variables are unique to a thread, unlike
instance and class variables. Therefore, if thread A gets a connection
locally then Threads B and C won't be accessing it.

AHS


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.