What is a transaction?
Two readings are provided to explain transactions and Oracle:
- The first, 1-page reading from your textbook Ensuring Transaction
Integrity gives an overview of transactions - The second, online reading Transaction
Management is from the Oracle
manual and details how Oracle implements transactions.
If you wish, you can now complete the Commit and Rollback tab of Getting your hands dirty.
Controlling concurrent access
Having seen how to ensure the integrity of an individual transaction, the next step is to ensure that simultaneous transactions do not interfere with each other. The reading, Controlling Concurrent Access, details the problems that can arise with concurrent access and some of the possible solutions.
Commit
and Rollback
Log on to SQL*Plus and try the following out on the INVOICE
or LIBRARY
database:
Using ROLLBACK
Read about COMMIT
and ROLLBACK
in the Oracle manual. Ignore the references made to distributed
transactions.
- Make a change to the database such as adding a new record (a
SELECT
statement is not a modification). - View your new record e.g. use a
SELECT
statement. ROLLBACK
the change.- Rerun your
SELECT
statement from step 2 to see your new record
What do you see?
The database is in its original state, that is the record you added is no longer there.
Using COMMIT
- Add the record from step 1 above
- View your change
-
COMMIT
the change -
ROLLBACK
the change. - Rerun your
SELECT
statement to view the change
What do you see?
The database is changed as the record you added is there. Once a
transaction has been committed it can not be undone with the ROLLBACK
statement.
Using SAVEPOINT
Read about SAVEPOINTS
in the Oracle manual.
- Make a change to your database e.g. add another record
- View the change
- Add a
SAVEPOINT
- Make a second change i.e. add a second new record to the same table
- View both changes
-
ROLLBACK
to theSAVEPOINT
- Rerun your
SELECT
statement to view the change
What do you see?
Only the first change is there as the second change has now been undone.
Concurrent access
Form yourself into pairs to investigate how Oracle handles concurrent access.
One reader, one writer
- Using the
GRANT
command, give your partner write access to one of your tables. - At the same time view the table, that is
issue a
SELECT
query simultaneously. Can you both view the table? - One of you now update the table (e.g. execute
an
UPDATE
statement). Do notCOMMIT
the changes. - Both try and view the change. Can you both view the change?
-
COMMIT
the change. - Both try and view the change. Can you both view the change?
Two writers
1. Both try and update the table at the same time. Do not COMMIT
the changes.
What happens? Why?
2. COMMIT
the change.
What happens? Why?
Locking level
With your partner, work out what locking level Oracle has in place when a user tries to update one row. If necessary you may need to review the section on Locking Level p. 520 in your textbook.