DFo 6 5
DFo 6 5
6-5
Transaction Control Language (TCL)
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 3
Transaction Control Language (TCL)
Application Express
• COMMIT, ROLLBACK, and SAVEPOINT are not
supported in Oracle Application Express, due to the
way Oracle Application Express manages connections
to the database
Oracle APEX
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 4
Transaction Control Language (TCL)
Database Transactions
• Transactions consist of DML statements that represent
one consistent change to the data
• The Oracle server ensures data consistency based on
transactions
• Transactions give you more flexibility and control when
you change data, and they ensure data consistency in
the event of user process failure or system failure
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 5
Transaction Control Language (TCL)
Database Transactions
• For example, a transfer of funds between two accounts
should include the debit in one account and the credit
to another account in the same amount. Both actions
should either fail or succeed together; the credit
should not be committed without the debit.
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 6
Transaction Control Language (TCL)
Database Transactions
• A database transaction consists of one of the following
statements:
−DML statements that represent one consistent change to the
data
−One DDL statement
−One TCL statement
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 7
Transaction Control Language (TCL)
Database Transactions: Start and End
• A transaction begins when the first DML SQL statement
is executed
• It ends with one of the following events:
−A COMMIT or ROLLBACK statement is issued
−A DDL or TCL statement executes (automatic commit)
−The user exits SQL software being used
−The system crashes
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 8
Transaction Control Language (TCL)
Advantages of COMMIT and ROLLBACK Statements
• With COMMIT and ROLLBACK statements, you can:
−Ensure data consistency
−Preview data changes before making changes permanent
−Group logically related operations
−Have control over permanent changes to the data
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 9
Transaction Control Language (TCL)
Transaction Control Statements
Statement Description
COMMIT Ends the current transaction by making all pending data changes
permanent.
SAVEPOINT Marks a savepoint within the current transaction.
name
ROLLBACK Ends the current transaction by discarding all pending data
changes.
ROLLBACK Rolls back the current transaction to the specified savepoint,
TO thereby discarding any changes and/or savepoints that were
SAVEPOINT created after the savepoint to which you are rolling back. If you
name omit the TO SAVEPOINT clause, the ROLLBACK statement rolls
back the entire transaction. Because savepoints are logical, there
is no way to list the savepoints that you created.
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 10
Transaction Control Language (TCL)
Explicit Transaction Control Statements
Time COMMIT
Transaction
DELETE FROM copy_employees;
SAVEPOINT A
UPDATE employees
SET dept_id = 110;
SAVEPOINT B
INSERT …
ROLLBACK ROLLBACK ROLLBACK
to SAVEPOINT B to SAVEPOINT A
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 11
Transaction Control Language (TCL)
Rolling Back Changes to a Marker
• Create a marker in the current transaction by using the
SAVEPOINT statement
• Discard pending changes by rolling back to that marker
by using the ROLLBACK TO SAVEPOINT statement
UPDATE...
SAVEPOINT update_done;
INSERT...
ROLLBACK TO update_done;
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 12
Transaction Control Language (TCL)
Implicit Transaction Processing
• An automatic commit occurs under the following
circumstances:
−A DDL statement is issued
−A TCL statement is issued
−There is a normal exit from SQL software, without explicitly
issuing COMMIT or ROLLBACK statements
• An automatic rollback occurs when there is an
abnormal termination of SQL software or when there is
a system failure to protect database integrity
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 13
Transaction Control Language (TCL)
State of the Data Before COMMIT or ROLLBACK
• Every data change made during a transaction is
temporary if not committed therefore the previous
state of the data can be recovered
• The current session can review the results of the DML
operations by using the SELECT statement
• Other sessions cannot view the results of the DML
statements issued by the current session
• The affected rows are locked; other sessions cannot
change the data in the affected rows
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 14
Transaction Control Language (TCL)
State of the Data After COMMIT
• Data changes are saved in the database
• The previous state of the data is overwritten
• All sessions can view the results
• Locks on the affected rows are released; those rows
are available for other sessions to manipulate
• All savepoints are erased
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 15
Transaction Control Language (TCL)
Committing Data
• Make the changes:
DELETE
FROM copy_employees
WHERE employee_id=113;
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 16
Transaction Control Language (TCL)
State of the Data After ROLLBACK
• Discard all pending changes by using the ROLLBACK
statement:
−Data changes are undone
−Previous state of the data is restored
−Locks on the affected rows are released
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 17
Transaction Control Language (TCL)
State of the Data After ROLLBACK: Example
• Assume there is a test table containing 4 records:
DELETE FROM test;
4 rows deleted
ROLLBACK;
Rollback complete
DELETE FROM test WHERE id = 100;
1 row deleted
SELECT * FROM test WHERE id = 100;
No rows selected
COMMIT;
Commit complete
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 18
Transaction Control Language (TCL)
Statement-Level Rollback
• If a single DML statement fails during execution, only
that statement is rolled back
• The Oracle server implements an implicit savepoint
• All other changes are retained
• The user should terminate transactions explicitly by
executing a COMMIT or a ROLLBACK statement
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 19
Transaction Control Language (TCL)
Read Consistency
• Read consistency guarantees a consistent view of the
data at all times – each user sees data as it existed as
of the last COMMIT
• Changes made by one user do not conflict with the
changes made by another user
• Read consistency ensures that, on the same data:
−Readers do not wait for writers
−Writers do not wait for readers
−Writers wait for other writers
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 20
Transaction Control Language (TCL)
Implementing Read Consistency
UPDATE employees Data
SET salary = 7000 blocks
WHERE last_name = 'Grant';
Undo
segments
User A
Changed
SELECT * Read- and
unchanged
FROM userA.employees; consistent data
image Before
change
User B ("old" data)
Note : for a more detailed explanation of read consistency see slide notes
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 21
Transaction Control Language (TCL)
Summary
• In this lesson, you should have learned how to:
−Describe the purpose of the Transaction Control Language
(TCL)
−Explain the TCL operations that are required to manage a
transaction:
• COMMIT
• SAVEPOINT
• ROLLBACK
−Describe the need for read
consistency
DFo 6-5 Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 22
Transaction Control Language (TCL)