The following tutorial continues the same scenario. In this case, the Accountant views the Departments table while the Sales
Manager creates a new department. You will observe the appearance of a phantom row.
If you have not done so, do steps 1 through 4 of the previous tutorial, Tutorial: Non-repeatable reads, so that you have two instances of Interactive SQL.
- Set the isolation level to 2 in the Sales Manager window by executing the following command.
- Set the isolation level to 2 for the Accountant window by executing the following command.
- In the Accountant window, enter the following command to list all the departments.
- The Sales Manager decides to set up a new department to focus on the foreign market. Philip Chin, who has EmployeeID 129,
heads the new department.
The final command creates the new entry for the new department. It appears as a new row at the bottom of the table in the
Sales Manager's window.
In the Sales Manager window, enter the following command to list all the departments.
- The Accountant, however, is not aware of the new department. At isolation level 2, the database server places locks to ensure
that no row changes, but places no locks that stop other transactions from inserting new rows.
The Accountant will only discover the new row if he executes his SELECT command again. In the Accountant's window, execute
the SELECT statement again. You will see the new row appended to the table.
The new row that appears is called a phantom row because, from the Accountant's point of view, it appears like an apparition, seemingly from nowhere. The Accountant is connected
at isolation level 2. At that level, the database server acquires locks only on the rows that he is using. Other rows are
left untouched and hence there is nothing to prevent the Sales Manager from inserting a new row.
- The Accountant would prefer to avoid such surprises in future, so he raises the isolation level of his current transaction
to level 3. Enter the following commands for the Accountant.
- The Sales Manager would like to add a second department to handle a sales initiative aimed at large corporate partners. Execute
the following command in the Sales Manager's window.
The Sales Manager's window pauses during execution because the Accountant's locks block the command. From the toolbar, click
Interrupt The SQL Statement (or choose SQL » Stop) to interrupt this entry.
- To avoid changing the SQL Anywhere sample database, you should roll back the incomplete transaction that inserts the Major
Account Sales department row and use a second transaction to delete the Foreign Sales department.
- Execute the following command in the Sales Manager's window to rollback the last, incomplete transaction:
- Also in the Sales Manager's window, execute the following two statements to delete the row that you inserted earlier and commit
this operation.
Explanation
When the Accountant raised his isolation to level 3 and again selected all rows in the Departments table, the database server
placed anti-insert locks on each row in the table, and added one extra phantom lock to block inserts at the end of the table.
When the Sales Manager attempted to insert a new row at the end of the table, it was this final lock that blocked her command.
Notice that the Sales Manager's command was blocked even though she is still connected at isolation level 2. The database
server places anti-insert locks, like read locks, as demanded by the isolation level and statements of each transactions.
Once placed, these locks must be respected by all other concurrent transactions.
For more information about locking, see How locking works.
Using snapshot isolation to avoid phantom rows
You can use the snapshot isolation level to maintain consistency at the same level as isolation level at 3, without any sort
of blocking. The Sales Manager's command is not blocked, and the Accountant does not see a phantom row.
If you have not done so, follow steps 1 through 4 of the Tutorial: Phantom rows which describe how to start two instances of Interactive SQL.
- Enable snapshot isolation for the Accountant by executing the following command.
- In the Accountant window, enter the following command to list all the departments.
- The Sales Manager decides to set up a new department to focus on the foreign market. Philip Chin, who has EmployeeID 129,
heads the new department.
The final command creates the new entry for the new department. It appears as a new row at the bottom of the table in the
Sales Manager's window.
- The Accountant can execute his query again, and does not see the new row because the transaction has not ended.
- The Sales Manager would like to add a second department to handle sales initiative aimed at large corporate partners. Execute
the following command in the Sales Manager's window.
The Sales Manager's change is not blocked because the Accountant is using snapshot isolation.
- The Accountant must end his snapshot transaction to see the changes the Sales Manager committed to the database.
Now the Accountant sees the Foreign Sales department, but not the Major Account Sales department.
- To avoid changing the SQL Anywhere sample database, you should roll back the incomplete transaction that inserts the Major
Account Sales department row and use a second transaction to delete the Foreign Sales department.
- Execute the following command in the Sales Manager's window to rollback the last, incomplete transaction:
- Also in the Sales Manager's window, execute the following two statements to delete the row that you inserted earlier and commit
this operation.