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 phantom locks

In this tutorial the Accountant and the Sales Manager both have tasks that involve the SalesOrder and SalesOrderItems tables. The Accountant needs to verify the amounts of the commission checks paid to the sales employees while the Sales Manager notices that some orders are missing and wants to add them.

Prerequisites

You must have the SELECT ANY TABLE, INSERT ANY TABLE, and DELETE ANY TABLE system privileges.

This tutorial assumes that you have connected to the sample database as the Sales Manager and as the Accountant. See Tutorial: Setting up the scenario for the isolation level tutorials.

Note

For this tutorial to work properly, the Automatically Release Database Locks option must not be selected in Interactive SQL. You can check the setting of this option by clicking Tools » Options, and then clicking SQL Anywhere in the left pane.

Context and remarks

This tutorial demonstrates phantom locking. A phantom lock is a shared lock that is placed on an indexed scan position to prevent phantom rows. When a transaction at isolation level 3 selects rows that match the specified criteria, the database server places anti-insert locks to stop other transactions from inserting rows that would also match. The number of locks placed on your behalf depends both on the search criteria and on the design of your database.

 Task
  1. Set the isolation level to 2 in both the Sales Manager and Accountant windows by executing the following statement in each:

    SET TEMPORARY OPTION isolation_level = 2;
  2. Each month, the sales representatives are paid a commission that is calculated as a percentage of their sales for that month. The Accountant is preparing the commission checks for the month of April 2001. His first task is to calculate the total sales of each representative during this month. Prices, sales order information, and employee data are stored in separate tables. Join these tables using the foreign key relationships to combine the necessary pieces of information.

    As the Accountant, execute the following statement:



    SELECT EmployeeID, GivenName, Surname,
       SUM( SalesOrderItems.Quantity * UnitPrice )
          AS "April sales"
    FROM GROUPO.Employees
       KEY JOIN GROUPO.SalesOrders
       KEY JOIN GROUPO.SalesOrderItems
       KEY JOIN GROUPO.Products
    WHERE '2001-04-01' <= OrderDate
       AND OrderDate < '2001-05-01'
    GROUP BY  EmployeeID, GivenName, Surname
    ORDER BY EmployeeID;
    EmployeeID GivenName Surname April sales
    129 Philip Chin 2160.00
    195 Marc Dill 2568.00
    299 Rollin Overbey 5760.00
    467 James Klobucher 3228.00
    ... ... ... ...
  3. The Sales Manager notices that a big order sold by Philip Chin was not entered into the database. Philip likes to be paid his commission promptly, so the Sales Manager enters the missing order, which was placed on April 25.

    As the Sales Manager, execute the following statements. The sales order and the items are entered in separate tables because one order can contain many items. You should create the entry for the sales order before you add items to it. To maintain referential integrity, the database server allows a transaction to add items to an order only if that order already exists.

    INSERT into GROUPO.SalesOrders
    VALUES ( 2653, 174, '2001-04-22', 'r1',
          'Central', 129 );
    INSERT into GROUPO.SalesOrderItems
    VALUES ( 2653, 1, 601, 100, '2001-04-25' );
    COMMIT;
  4. The Accountant has no way of knowing that the Sales Manager has just added a new order. Had the new order been entered earlier, it would have been included in the calculation of Philip Chin's April sales.

    In the Accountant's window, calculate the April sales totals again. Use the same statement, and observe that Philip Chin's April sales changes to $4560.00.

    EmployeeID GivenName Surname April sales
    129 Philip Chin 4560.00
    195 Marc Dill 2568.00
    299 Rollin Overbey 5760.00
    467 James Klobucher 3228.00
    ... ... ... ...

    Imagine that the Accountant now marks all orders placed in April to indicate that commission has been paid. The order that the Sales Manager just entered might be found in the second search and marked as paid, even though it was not included in Philip's total April sales.

  5. At isolation level 3, the database server places anti-insert locks to ensure that no other transactions can add a row that matches the criteria of a search or select.

    As the Sales Manager, execute the following statements to remove the new order:

    DELETE
    FROM GROUPO.SalesOrderItems
    WHERE ID = 2653;
    DELETE
    FROM GROUPO.SalesOrders
    WHERE ID = 2653;
    COMMIT;
  6. As the Accountant, execute the following statements:

    ROLLBACK;
    SET TEMPORARY OPTION isolation_level = 3;
  7. Execute the following query:



    SELECT EmployeeID, GivenName, Surname,
       SUM( SalesOrderItems.Quantity * UnitPrice )
          AS "April sales"
    FROM GROUPO.Employees
       KEY JOIN GROUPO.SalesOrders
       KEY JOIN GROUPO.SalesOrderItems
       KEY JOIN GROUPO.Products
    WHERE '2001-04-01' <= OrderDate
       AND OrderDate < '2001-05-01'
    GROUP BY  EmployeeID, GivenName, Surname;

    Because you set the isolation to level 3, the database server automatically places anti-insert locks to ensure that the Sales Manager cannot insert April order items until the Accountant finishes his transaction.

  8. As the Sales Manager, attempt to enter Philip Chin's missing order by executing the following statement:

    INSERT INTO GROUPO.SalesOrders
    VALUES ( 2653, 174, '2001-04-22',
             'r1','Central', 129 );

    The Sales Manager's window stops responding, and the operation does not complete. On the toolbar, click Stop to interrupt this entry.

  9. The Sales Manager cannot enter the order in April, but you might think that they could still enter it in May.

    Change the date in the statement to May 05 and try again.

    INSERT INTO GROUPO.SalesOrders
    VALUES ( 2653, 174, '2001-05-05', 'r1',
          'Central', 129 );

    The Sales Manager's window stops responding again. On the toolbar, click Stop to interrupt this entry. Although the database server places no more locks than necessary to prevent insertions, these locks have the potential to interfere with many transactions.

    The database server places locks in table indexes. For example, it places a phantom lock in an index so a new row cannot be inserted immediately before it. However, when no suitable index is present, it must lock every row in the table. In some situations, anti-insert locks may block some insertions into a table, yet allow others.

  10. To avoid changing the SQL Anywhere sample database, you should roll back the changes made to the SalesOrders table. In both the Sales Manager and Accountant windows, execute the following statement:

    ROLLBACK;
  11. Shut down both instances of Interactive SQL.

Results

You have completed the tutorial on understanding how phantom locks work.

 See also