Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Databases > Re: thread safe...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 3 Topic 3684 of 3800
Post > Topic >>

Re: thread safe JDBC code

by "Arved Sandstrom" <asandstrom@[EMAIL PROTECTED] > Mar 25, 2008 at 05:56 PM

"Steve" <javacc2@[EMAIL PROTECTED]
> wrote in message 
news:4cf5dc4a-e127-42f0-96be-b95c437512f3@[EMAIL PROTECTED]
> 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
 




 3 Posts in Topic:
thread safe JDBC code
Steve <javacc2@[EMAIL   2008-03-23 16:40:53 
Re: thread safe JDBC code
=?ISO-8859-1?Q?Arne_Vajh=  2008-03-23 20:46:43 
Re: thread safe JDBC code
"Arved Sandstrom&quo  2008-03-25 17:56:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 23:15:57 CDT 2008.