|
|
Question : Row lock and transaction
|
|
Does Sybase SQL Anywhere 8 support row lock? I'm writing a MFC application that already implemet row lock with Oracle via ODBC. In Oracle I use:
1. Start a transaction 2. Lock a record (SELECT * FROM Table WHERE ID = 1 FOR UPDATE NOWAIT) 3. Wait for a user record's modify 4. Commit
If another user tries to modify the record, Oracle returns an error message. Is it possible to implement this with Sybase' server? Thank you
|
Answer : Row lock and transaction
|
|
Yes, you can achieve this in the following way:
update table1 set col1=col1 where pkcol = 'xyz'
This will give you a write lock on that particular record in the table, a commit will then release the lock.
Having said that the best results are probably achieved by letting the database engine deal with the locking. One of the keys is setting the isolation level for the database or the connection. There are four level:
0 - Dirty reads, phantom rowes and non-repeatable reads are allowed 1 - same as 0 except dirty reads are prevented 2 - same as 1 expect non-repeatable rows are prevented 3 - same as 2 except phantom rows are prevented
Use SET OPTION ISOLATION_LEVEL = x to do it for the database or SET TEMPORARY OPTION ISOLATION_LEVEL = x for the current connection. Use the sa_locks stored procedure to check that it is working ok. You can compare the connection with that shows in sa_conn_info which shows which connection has the lock.
In addition use the BLOCKING option to control whether blocked transactions wait for the commit or error.
|
|
|
|