You can also use snapshot isolation to help avoid blocking. Because transactions that use snapshot isolation only see committed data, the Accountant's transaction does not block the Sales Manager's transaction.
Start Interactive SQL.
In the Connect window, connect to the SQL Anywhere sample database as the Sales Manager:
From the Action dropdown list, click Connect to an ODBC Data Source.
Click ODBC Data Source Name, and then type SQL Anywhere 12 Demo in the field below.
If necessary, click Advanced, to reveal the Advanced Options tab.
Click the Advanced Options tab and type Sales Manager in the ConnectionName field.
Click Connect.
Start a second instance of Interactive SQL.
In the Connect window, connect to the SQL Anywhere sample database as the Accountant:
In the Action dropdown list, click Connect With An ODBC Data Source.
Click ODBC Data Source Name, and then type SQL Anywhere 12 Demo in the field below.
If necessary, click Advanced button, to reveal the Advanced Options tab.
Click the Advanced Options tab and type Accountant in the ConnectionName field.
Click Connect.
As the Accountant, execute the following statements to enable snapshot isolation for the database and specify that the snapshot isolation level is used:
SET OPTION PUBLIC.allow_snapshot_isolation = 'On'; SET TEMPORARY OPTION isolation_level = 'snapshot'; |
The Accountant decides to list the prices of the visors. As the Accountant, execute the following statement:
SELECT ID, Name, UnitPrice FROM Products ORDER BY ID; |
ID | Name | UnitPrice |
---|---|---|
300 | Tee Shirt | 9.00 |
301 | Tee Shirt | 14.00 |
302 | Tee Shirt | 14.00 |
400 | Baseball Cap | 9.00 |
401 | Baseball Cap | 10.00 |
500 | Visor | 7.00 |
501 | Visor | 7.00 |
... | ... | ... |
The Sales Manager decides to introduce a new sale price for the plastic visor. As the Sales Manager, execute the following statements:
UPDATE Products SET UnitPrice = 5.95 WHERE ID = 501; COMMIT; SELECT ID, Name, UnitPrice FROM Products WHERE Name = 'Visor'; |
The Accountant executes his query again, and does not see the change in price because the data that was committed at the time of the first read is used for the transaction.
SELECT ID, Name, UnitPrice FROM Products; |
As the Sales Manager, change the plastic visor back to its original price.
UPDATE Products SET UnitPrice = 7.00 WHERE ID = 501; COMMIT; |
The database server does not place a read lock on the rows in the Products table that the Accountant is reading because the Accountant is viewing a snapshot of committed data that was taken before the Sales Manager made any changes to the Products table.
The Accountant is finished looking at the prices. He doesn't want to risk accidentally changing the database, so he completes his transaction with a ROLLBACK statement.
ROLLBACK; |
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |