How can I place a lock on a single row?

Geoff Sovde

First, not all databases support row level locks, so check the documentation for your DMBS. If you are using Oracle, you can issue a select...for update statement. Make sure your JDBC connection has autocommit turned off- otherwise, you'll get an ORA-01002- "Fetch out of sequence" error. Here's an example:

String cmd = "SELECT * FROM my_table WHERE my_column_a = criteria FOR UPDATE OF my_column_b";

ResultSet rset = stmt.executeQuery(cmd);
Zac Corbiere adds

The important bit is the 'for update' tagged on the end.

From your resultset invoke 'rs.getCursorName' and pass that back in to an update statement with 'where current of' like:
String updateStmt = "update foo set bar="FUBAR" where current of " + rs.getCursorName();
Comment and Contribute

 

 

 

 

 


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

 

 

About | Sitemap | Contact