The following tutorial demonstrates one type of inconsistency that can occur when multiple transactions are executed concurrently.
Two employees at a small merchandising company access the corporate database at the same time. The first person is the company's
Sales Manager. The second is the Accountant.
The Sales Manager wants to increase the price of tee shirts sold by their firm by $0.95, but is having a little trouble with
the syntax of the SQL language. At the same time, unknown to the Sales Manager, the Accountant is trying to calculate the
retail value of the current inventory to include in a report he volunteered to bring to the next management meeting.
Tip
Before altering your database in the following way, it is prudent to test the change by using SELECT in place of UPDATE.
In this example, you will play the role of two people, both using the SQL Anywhere sample database concurrently.
- Start Interactive SQL.
- In the Connect window, connect to the SQL Anywhere sample database as the Sales Manager:
- In the ODBC Data Source Name field, choose SQL Anywhere 11 Demo.
- Click the Advanced tab, and type Sales Manager in the ConnectionName field.
- Click OK.
- Start a second instance of Interactive SQL.
- In the Connect window, connect to the SQL Anywhere sample database as the Accountant:
- In the ODBC Data Source Name field, choose SQL Anywhere 11 Demo.
- Click the Advanced tab and type Accountant in the ConnectionName field.
- Click OK.
- As the Sales Manager, raise the price of all tee shirts by $0.95:
- In the Sales Manager window, execute the following commands:
The result is:
You observe immediately that you should have entered 0.95 instead of 95, but before you can fix your error, the Accountant
accesses the database from another office.
- The company's Accountant is worried that too much money is tied up in inventory. As the Accountant, execute the following
commands to calculate the total retail value of all the merchandise in stock:
The result is:
Unfortunately, this calculation is not accurate. The Sales Manager accidentally raised the price of the visor $95, and the
result reflects this erroneous price. This mistake demonstrates one typical type of inconsistency known as a dirty read. You, as the Accountant, accessed data which the Sales Manager has entered, but has not yet committed.
You can eliminate dirty reads and other inconsistencies explained in Isolation levels and consistency.
- As the Sales Manager, fix the error by rolling back your first changes and entering the correct UPDATE command. Check that
your new values are correct.
- The Accountant does not know that the amount he calculated was in error. You can see the correct value by executing the SELECT
statement again in the Accountant's window.
- Finish the transaction in the Sales Manager's window. The Sales Manager would enter a COMMIT statement to make the changes
permanent, but you should execute a ROLLBACK, instead, to avoid changing the local copy of the SQL Anywhere sample database.
The Accountant unknowingly receives erroneous information from the database because the database server is processing the
work of both the Sales Manager and the Accountant concurrently.
Using snapshot isolation to avoid dirty reads
When you use snapshot isolation, other database connections see only committed data in response to their queries. Setting
the isolation level to statement-snapshot or snapshot prevents the possibility of dirty reads occurring. The Accountant can
use snapshot isolation to ensure that they only see committed data when executing their queries.
- Start Interactive SQL.
- In the Connect window, connect to the SQL Anywhere sample database as the Sales Manager:
- In the ODBC Data Source Name field, choose SQL Anywhere 11 Demo.
- Click the Advanced tab and type Sales Manager in the ConnectionName field.
- Click OK to connect.
- Execute the following statement to enable snapshot isolation for the database:
- Start a second instance of Interactive SQL.
- In the Connect window, connect to the SQL Anywhere sample database as the Accountant:
- In the ODBC Data Source Name field, choose SQL Anywhere 11 Demo.
- Click the Advanced tab and type Accountant in the ConnectionName field.
- Click OK.
- As the Sales Manager, raise the price of all the tee shirts by $0.95:
- In the window labeled Sales Manager, execute the following command to:
- Calculate the total retail value of all merchandise in stock using the new tee shirt price for the Sales Manager:
The result is:
- As the Accountant, execute the following command to calculate the total retail value of all the merchandise in stock. Because
this transaction uses the snapshot isolation level, the result is calculated only for data that has been committed to the
database.
The result is:
- As the Sales Manager, commit your changes to the database by executing the following statement:
- As the Accountant, execute the following statements to view the updated retail value of the current inventory:
The result is:
Because the snapshot used for the Accountant's transaction began with the first read operation, you must execute a COMMIT
to end the transaction and allow the Accountant to see changes made to the data after the snapshot transaction began. See
Understanding snapshot transactions.
- As the Sales Manager, execute the following statement to undo the tee shirt price changes and restore the SQL Anywhere sample
database to its original state: