Chapter 12: Manage Transactions, Error Handling & Dynamic SQL Flashcards
What’s a transaction?
It’s an unit of work: it’s all done or none, that’s partial result it’s not acceptable, it’s all or nothing.
What’s ACID?
It’s the properties that defines an database transaction. ACID stands for:
A: Atomicity. Every transaction is an atomic unit of work, that is, all database changes in the transactions are succeed or none of them succeed.
C: Consistency. Every transaction, whether successful or not, need to leave the database in a consistent state. If a inconsistent occur when a transaction happens, then it’ll rollback to the previous state, where the consistency was.
I: Isolated. A transaction don’t need to interference with another database transactions.
D: Durability. This property ensures that once the transaction has completed execution, the updates and modifications to the database are stored in and written to disk and they persist even is system failure occurs. These updates now become permanent and are stored in a non-volatile memory. The effects of the transaction, thus, are never lost.
Why is it important for SQL Server to maintain the ACID quality of transactions?
To ensure that the integrity of database data will not be compromised.
How does SQL Server implement transaction durability?
By first writing all changes to the database transaction log before making changes to the database data.
What are the types of transactions?
System transactions and user transactions.
What are the typicals commands for handle transaction?
BEGIN TRANSACTION tells that all codes below belong to this transaction.
COMMIT TRANSACTION tells that the changes needs to be commited to the database.
ROLLBACK TRANSACTION tells that all the changes will be reversed.
What are the functions to know the transaction level and its states?
@@TRANCOUNT and XACT_STATE()
What are the modes to work with SQL Server database transactions?
Autocommit mode,
Implicit Transaction mode,
Explicit Transaction mode
What does do the @@TRANCOUNT?
Returns the number of BEGIN TRANSACTION statements that have occurred on the current connection.
The BEGIN TRANSACTION statement increments @@TRANCOUNT by 1. ROLLBACK TRANSACTION decrements @@TRANCOUNT to 0, except for ROLLBACK TRANSACTION savepoint_name, which does not affect @@TRANCOUNT. COMMIT TRANSACTION or COMMIT WORK decrement @@TRANCOUNT by 1.
Review and practice
IMPLICIT TRANSACTION MODE AND EXPLICIT TRANSACTION MODE
What’s a nested transaction?
When a transaction contains another transaction inside them, and so on.
Exam tip about nested transaction and COMMIT
An inner COMMIT statement has no real effect on the transaction, only decrementing
@@TRANCOUNT by 1. Just the outermost COMMIT statement, the one executed when
@@TRANCOUNT = 1, actually commits the transaction.
Exam tip about nested transaction and ROLLBACK
Note that it doesn’t matter at what level you issue the ROLLBACK command. A transaction
can contain only one ROLLBACK command, and it will roll back the entire transaction and
reset the @@TRANCOUNT counter to 0.
What are the aditional transaction options?
Savepoints, Cross - database transactions and Distributed transactions.
How many ROLLBACKs must be executed in a nested transaction to roll it back?
Only one ROLLBACK. A ROLLBACK always rolls back the entire transaction, no matter how many levels the transaction has.