Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » SQL Anywhere Server - SQL Usage » Transactions and isolation levels » Isolation level tutorials » Tutorial: Understanding non-repeatable reads

 

Lesson 1: Creating a non-repeatable read

Create a non-repeatable read, in which the Accountant attempts to read a row being modified by the Sales Manager and gets two different results during the same transaction.

Prerequisites

This lesson assumes that you have the roles and privileges listed in the Privileges section at the start of this tutorial: Tutorial: Understanding non-repeatable reads.

 Task
  1. Set the isolation level to 1 for the Accountant's connection by executing the following statement:

    SET TEMPORARY OPTION isolation_level = 1;
  2. Set the isolation level to 1 in the Sales Manager's window by executing the following statement:

    SET TEMPORARY OPTION isolation_level = 1;
  3. As the Accountant, execute the following statement to list the prices of the visors:

    SELECT ID, Name, UnitPrice 
    FROM GROUPO.Products;
    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
    ... ... ...
  4. As the Sales Manager, execute the following statements to introduce a new sale price for the plastic visor:

    SELECT ID, Name, UnitPrice FROM GROUPO.Products
    WHERE Name = 'Visor';
    UPDATE GROUPO.Products
    SET UnitPrice = 5.95 WHERE ID = 501;
    COMMIT;
    SELECT ID, Name, UnitPrice FROM GROUPO.Products
    WHERE Name = 'Visor';
    ID Name UnitPrice
    500 Visor 7.00
    501 Visor 5.95
  5. Compare the price of the visor in the Sales Manager window with the price for the same visor in the Accountant window. As the Accountant, execute the SELECT statement again and see the Sales Manager's new sale price:

    SELECT ID, Name, UnitPrice
    FROM GROUPO.Products;
    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 5.95
    ... ... ...

    This inconsistency is called a non-repeatable read because after executing the same SELECT a second time in the same transaction, the Accountant did not get the same results.

    Of course, if the Accountant had finished the transaction, for example by issuing a COMMIT or ROLLBACK statement before using SELECT again, it would be a different matter. The database is available for simultaneous use by multiple users and it is completely permissible for someone to change values either before or after the Accountant's transaction. The change in results is only inconsistent because it happens in the middle of the transaction. Such an event makes the schedule unserializable.

  6. The Accountant notices this behavior and decides that from now on he doesn't want the prices changing while he looks at them. Non-repeatable reads are eliminated at isolation level 2. As the Accountant, execute the following statements:

    SET TEMPORARY OPTION isolation_level = 2;
    SELECT ID, Name, UnitPrice
    FROM GROUPO.Products;
  7. The Sales Manager decides that it would be better to delay the sale on the plastic visor until next week so that she won't have to give the lower price on a big order that she's expecting to arrive tomorrow. As the Sales Manager, try to execute the following statements. The statement starts to execute, and then the window appears to freeze.

    UPDATE GROUPO.Products
    SET UnitPrice = 7.00
    WHERE ID = 501;

    The database server must guarantee repeatable reads at isolation level 2. Because the Accountant is using isolation level 2, the database server places a read lock on each row of the Products table that the Accountant reads. When the Sales Manager tries to change the price back, her transaction must acquire a write lock on the plastic visor row of the Products table. Since write locks are exclusive, her transaction must wait until the Accountant's transaction releases its read lock.

  8. 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;

    When the database server executes this statement, the Sales Manager's transaction completes.

    ID Name UnitPrice
    500 Visor 7.00
    501 Visor 7.00
  9. The Sales Manager can finish her transaction now. She wants to commit her change to restore the original price:

    COMMIT;

Results

The Accountant receives different results during the same transaction, so he enables snapshot isolation level 2 to avoid non-repeatable reads. However, the Accountant's change to the database blocks the Sales Manager from making any changes to the database.

When you upgraded the Accountant's isolation from level 1 to level 2, the database server used read locks where none had previously been acquired. From then on, it acquired a read lock for his transaction on each row that matched his selection.

In the above tutorial, the Sales Manager's window froze during the execution of her UPDATE statement. The database server began to execute her statement, then found that the Accountant's transaction had acquired a read lock on the row that the Sales Manager needed to change. At this point, the database server simply paused the execution of the UPDATE. Once the Accountant finished his transaction with the ROLLBACK, the database server automatically released his locks. Finding no further obstructions, the database server completed execution of the Sales Manager's UPDATE.

 See also