MySQL Forums
Forum List  »  General

Unclear on SELECT...FOR UPDATE in docs
Posted by: Michael Bruce
Date: October 14, 2004 07:05PM

In the manual, there is an example for using SELECT..FOR UPDATE. The documentation does not specify if the lock is cleared on the next call to UPDATE or if an explicit COMMIT must be called. Here's the example:

SELECT counter_field FROM child_codes FOR UPDATE;
UPDATE child_codes SET counter_field = counter_field + 1;

I am looking at using the C API with this. It seems to me that a COMMIT/ROLLBACK would be required to end the lock, because my application may decide not to do an UPDATE or do multiple UPDATEs after the SELECT. Is my assumption correct or is there some other way to cancel the FOR UPDATE lock? Here's some simplified sample code:

mysql_query(Conn, "SELECT counter_field FROM child_codes FOR UPDATE");
Result = mysql_store_result(Conn);
Row = mysql_fetch_row(Result);
if(WantUpdate) {
mysql_stmt_prepare(Statement, "UPDATE child_codes SET counter_field = counter_field + 1", 58 );
mysql_stmt_execute(Statement);
mysql_stmt_close(Statement);
mysql_commit(Conn);
} else {
mysql_rollback(Conn);
}

Also, does the auto commit setting have any effect on this? Thanks!

Options: ReplyQuote


Subject
Written By
Posted
Unclear on SELECT...FOR UPDATE in docs
October 14, 2004 07:05PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.