I have a static method where I pass some parameters and it does db manipulations, returning some resultset. Do I need to synchronize this method to make it thread safe? It does not use any other variables. It only uses local variables passed as method parameters.

Fredrik Appelberg

[No, not if you make a new java.sql.Statement for each request / thread. JDBC drivers are supposed to be multithreaded -- although early versions of some drivers were buggy in this regard. -Alex]

You could get into trouble if two threads are using the same Connection at the same time (at least I had this problem with an Oracle database), espececially if autocommit is turned off and both thread try to perform several operations before calling commit(). Also, if one thread is looping through a ResultSet and another thread makes a query using the same Statement (or PreparedStatement), the first ResultSet gets screwed up.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact