Lecture4_DBP2024
Lecture4_DBP2024
Transaction
Transaction
What is a Transaction?
A transaction is a set of operations performed so all operations are guaranteed
to succeed or fail as one unit.
The transaction is all or none : A common example of a transaction is the
process of transferring money from a checking account to a savings account
Transaction
How to implement Transaction Management in SQL Server?
• Transaction processing involves three steps.
Transaction State
Transaction
Types of Transactions in SQL Server
Local Transactions in SQL Server.
Auto Commit Transaction Mode (default)
Implicit Transaction Mode
Explicit Transaction Mode
Points to remember:
When the inner transactions do a commit, it does not physically commit the transaction. . So, only the
outer commit transaction commits the transaction.
Transaction
What happens when the inner transactions commit in SQL Server?
• There is something called transaction count whose value will decrease when the inner
transaction commits. We have a global variable called @@TRANCOUNT which holds the
value of the number of open transactions.
Output
Transaction
What is the use of nested transactions in SQL Server?
The nested transactions are basically used with checkpoints or save
points for a partial rollback in SQL Server.
What are SavePoints in SQL Server?
The SAVE TRANSACTION in SQL Server is used for dividing
(or) breaking a transaction into multiple units so that the user has a
chance of roll backing the transaction up to a specified point. That
means using SavePoints Transaction we can rollback a part of a
transaction instead of the entire transaction.
Transaction
Transaction
• SavePoint with Nested Transaction in SQL Server:
When we execute the above statement, it should roll back the 3rd and 4th records while the 1st and
2nd records should be committed into the database.
Transaction
SQL Server transaction savepoints with the same savepoint name
Here, we created two savepoints with the same name i.e. SavePoint1. When we roll back SavePoint1
then it will roll back the data from the 2nd save point.
Transaction
Example: Savepoints with the same savepoint name in SQL Server
Transaction
Transaction
Understanding ACID Properties
A transaction is characterized by four properties, often referred to as the ACID
properties: atomicity, consistency, isolation, and durability
Atomicity Property of a Transaction in SQL Server:
The Atomicity Property of a Transaction in SQL Server ensures that either all the
DML Statements (i.e. insert, update, delete) inside a transaction are completed
successfully or all of them are rolled back.
Consistency Property of a Transaction in SQL Server:
The Consistency Property of a Transaction in SQL Server ensures that the database
data is in a consistent state before the transaction started and also left the data in a
consistent state after the transaction is completed
Transaction
Isolation Property of a Transaction in SQL Server:
The Isolation Property of a Transaction in SQL Server ensures that the intermediate
state of a transaction is invisible to other transactions. The Data modifications made
by one transaction must be isolated from the data modifications made by all other
transactions. Most databases use locking to maintain transaction isolation
Durability Property of a Transaction in SQL Server:
The Durability Property of a Transaction in SQL Server ensures that once the
transaction is successfully completed, then the changes it made to the database will
be permanent. Even if there is a system failure or power failure or any abnormal
changes, it should safeguard the committed data.
Transaction
T-SQL Statements Allowed in a Transaction
You can use all T-SQL statements in a transaction, except for the
following statements: ALTER DATABASE, RECONFIGURE, BACKUP,
RESTORE, CREATE DATABASE, UPDATE STATISTICS, and DROP
DATABASE
https://www.c-sharpcorner.com/UploadFile/84c85b/understanding-
transactions-in-sql-server/
https://dotnettutorials.net/lesson/acid-properties-in-sql-server/
Transaction