Vous êtes sur la page 1sur 15

TRANSACTION LOG (.

ldf)
 maintains a physical record of all transactions

 The transaction log is a binary file.


 In the default database configuration, the transaction log
keeps a record of all database modifications and is never
cleared unless it is backed up or explicitly truncated by a
database administrator.
Pankaj Bhatia - MBA(IT)- LPU
 The transaction log is maintained on disk as one or
more physical files.

 Log files are configured dynamically by SQL Server


and are not configurable

Pankaj Bhatia - MBA(IT)- LPU


TRANSACTIONS
a single unit of data operations that can be controlled so that
either all the modifications in a transaction occur, or none
occur.

ATMs

In malls, petrol stations, retail chains…


Pankaj Bhatia - MBA(IT)- LPU
Auto-Commit
By default SQL Server connections use Auto-Commit Transactions
Balance = 1000
UPDATE SavingsAccount
Balance = 1000
SET Balance = Balance - 300
- 300
WHERE AccountID = ‘10023’
= 700

UPDATE SavingsAccount
Balance = 700
SET Balance = Balance + 500
+500
WHERE AccountID = ‘10023’
= 1200

Final Balance = 1200


Pankaj Bhatia - MBA(IT)- LPU
ERROR !!!
Balance = 1000
UPDATE SavingsAccount
Balance = 1000
SET Balance = Balance - 300
- 300
WHERE AccountID = ‘10023’
= 700

UPDATE SavingsAccount
Balance = 700
SET Balance = Balance + 500
+500
WHERE AccountID = ‘10023’
= 1200

Final Balance = 700


Customer loses Rs. 500
Pankaj Bhatia - MBA(IT)- LPU
Three ways of Executing Transactions

1. IMPLICIT
Uses try and catch block
2. EXPLICIT

3. AUTO COMMIT

Pankaj Bhatia - MBA(IT)- LPU


IMPLICIT

The ANSI standard for the Structured Query Language


specifies that no modifications should be made to data unless
explicitly committed.

SQL Server 2008 uses IMPLICIT_TRANSACTIONS


connection property

Pankaj Bhatia - MBA(IT)- LPU


SET IMPLICIT_TRANSACTIONS ON

BEGIN TRY

UPDATE CheckingAccount
SET Balance = Balance + 500
WHERE AccountID = ‘123456789-CK’
Unsaved
UPDATE SavingsAccount
SET Balance = Balance - 500
WHERE AccountID = ‘123456789-SV’

COMMIT TRANSACTION Changes saved

END TRY

BEGIN CATCH Seeks for any errors

ROLLBACK TRANSACTION
RAISERROR(’Account Transfer Failed’, 14,1) Prints message

END CATCH

Pankaj Bhatia - MBA(IT)- LPU


Observations

 IMPLICIT_TRANSACTIONS is set to ON - any data


modification will implicitly begin a transaction,

 but will not close the transaction.

 The transaction will remain open until it is explicitly


committed or rolled back.

Pankaj Bhatia - MBA(IT)- LPU


EXPLICIT

SQL Server uses property named BEGIN TRANSACTION to


begin the transaction

and an explicit COMMIT TRANSACTION or ROLLBACK


TRANSACTION to close the transaction

Pankaj Bhatia - MBA(IT)- LPU


BEGIN TRY

BEGIN TRANSACTION Explicitly used


UPDATE CheckingAccount
SET Balance = Balance + 500
WHERE AccountID = ‘123456789-CK’

UPDATE SavingsAccount
SET Balance = Balance - 500
WHERE AccountID = ‘123456789-SV’
COMMIT TRANSACTION

END TRY

BEGIN CATCH
ROLLBACK TRANSACTION
RAISERROR(’Account Transfer Failed’, 14,1)
END CATCH

Pankaj Bhatia - MBA(IT)- LPU


RECORDING TRANSACTIONS Data Page

CACHE
OR
SQL
SERVER HDD
SSMS Data Page

RAM (Buffer cache) CACHE (Buffer cache)

SQL Server modifies the page and simultaneously updates


transaction log

Pankaj Bhatia - MBA(IT)- LPU


CHECKPOINT
When issued, all dirty pages in the buffer cache are written to
the data file on disk.
BEGIN TRANSACTION 1
UPDATE ...
INSERT ...
UPDATE ...
COMMIT TRANSACTION 1 Safely written to disk
BEGIN TRANSACTION 2
INSERT ...
UPDATE ...

***CHECKPOINT***

BEGIN TRANSACTION 3
DELETE ...
UPDATE ... Modification in
COMMIT TRANSACTION 3 Transaction Log only
BEGIN TRANSACTION 4
UPDATE ...
***Server Power failure*** Pankaj Bhatia - MBA(IT)- LPU
Transaction Log Physical Characteristics
 The transaction log is implemented as a serialized,
sequential, rotary write-back log.

LSN : Log Sequence Number


The preferred method of clearing the log is by backing up the transaction log

Pankaj Bhatia - MBA(IT)- LPU


Thank You

Any Queries ???

Pankaj Bhatia - MBA(IT)- LPU

Vous aimerez peut-être aussi