dcsimg
Prepared Statements and synchronization
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jason_Austin
Posted On:   Thursday, February 27, 2003 09:32 AM

I'm not sure if this is really a struts question but thought I would ask here since all of the Struts books I have at some point deal with connection to a back-end database. I currently have a Data Access Layer where I've placed my SQL code inside of the java objects. Currently, I create an SQL string using sb.append(...) statements to construct the string. I've been reading that it's far better to use PreparedStatement objects for dealing with the database. Are PreparedStatement objects thread-safe (i.e. synchronized)? I want to ensure that if user A gets a hold of the prepared statement then user B can't execute it until user A is finsihed with it. Thanks for help in advance Jason   More>>

I'm not sure if this is really a struts question but thought I would ask here since all of the Struts books I have at some point deal with connection to a back-end database.


I currently have a Data Access Layer where I've placed my SQL code inside of the java objects. Currently, I create an SQL string using sb.append(...) statements to construct the string. I've been reading that it's far better to use PreparedStatement objects for dealing with the database.


Are PreparedStatement objects thread-safe (i.e. synchronized)? I want to ensure that if user A gets a hold of the prepared statement then user B can't execute it until user A is finsihed with it.


Thanks for help in advance


Jason

   <<Less

Re: Prepared Statements and synchronization

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, February 27, 2003 12:28 PM

Don't ever use a member field or class field to hold a PreparedStatement. Just always use a local variable, and then there is no chance for multiple threads to get ahold of the same copy. I don't think PreparedStatements should hang around for very long anyway, never beyond a single local block of code inside one method; you should close them as soon as you are done with them (indeed also close the ResultSet if any, and the Connection too, assuming it is a logical connection from a connection pool rather than a real physical connection).

About | Sitemap | Contact