LAB 3 Manual: Instructor: Saad Hameed Subject: Database Administration & Management Lab Class: BS-IT (4-A)
LAB 3 Manual: Instructor: Saad Hameed Subject: Database Administration & Management Lab Class: BS-IT (4-A)
LAB 3 Manual: Instructor: Saad Hameed Subject: Database Administration & Management Lab Class: BS-IT (4-A)
LAB 3 Manual
Overview
Transaction
Concurrency Problems
Dirty reads
Lost updates
Repeatable reads
Phantom reads
Isolation Levels
Read Uncommitted
Read Committed
Repeatable Read
Serializable
Snapshot
Transaction
if you are creating a record or updating a record or deleting a record from the table, then you are
performing a transaction on that table.
Update
Delete
Insert
select
It is important to control these transactions to ensure the data integrity and to handle database errors.
Types of Transactions
Explicit Transactions
An explicit transaction is one in which you explicitly define both the start and end of the transaction.
This can be specified by using either SQL statements or database API functions.
SQL Statements
By using Visual Studio, the following SQL statements can be used to define explicit transactions:
BEGIN TRANSACTION
Marks the starting point of an explicit transaction for a connection.
COMMIT TRANSACTION
Ends a transaction successfully if no errors are encountered. All data modified by the transaction
becomes a permanent part of the database. Resources held by the transaction are freed.
ROLLBACK TRANSACTION
Erases a transaction in which errors are encountered. All data modified by the transaction is
returned to the state it was in at the start of the transaction. Resources held by the transaction are
freed.
Autocommit Transactions
Autocommit mode is the default transaction management mode of SQL Server Compact. Every SQL
statement is committed or rolled back when it finishes. A SQL Server Compact connection operates in
autocommit mode whenever this default mode has not been overridden by explicit transactions.
Autocommit mode is also the default mode for ADO.NET and OLE DB.
A SQL Server Compact connection operates in autocommit mode until a BEGIN
TRANSACTION statement starts an explicit transaction. When the explicit transaction is committed or
rolled back, SQL Server Compact returns to autocommit mode.
Example
Example
Try Catch
Task#1
TRANSFER BALANCE FROM ONE CUSTOMER ACCOUNT INTO ANOTHER CUSTOMER ACCOUNT IF TRANSACTION
FAILED DUE TO SOME input ERROR(ERROR COULD BE ANY),TRANSACTION SHOULD ROLL BACK AND IF
TRANSFER IS SUCCESSFUL TRRANSACTION SHOULD GET COMMIT, hint: Use try catch
Example
--Transaction 1
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
BEGIN TRAN
WAITFOR DELAY '00:00:10'
insert into Accounts(Name,Balance,DateChanged)
values('NewEmployee',(select Balance from Accounts where Acc_Id=1),'12-2-2014')
Concurrency
Concurrency is the ability of multiple users to access data at the same time.
Concurrency Problems
If you do not manage the modification and reading of data by multiple users, concurrency problems can
occur. For example, if several users access a database at the same time, their transactions could try to
perform operations on the same data at the same time. Some of the concurrency problems that occur
while using SQL Server include the following:
Dirty reads:- This situation happens when a transaction tries to read a data by some other
concurrent transaction which is not committed yet. There is a risk, that this other transaction
may never be committed, leaving the original transaction with wrong data.
example
Example
--Transaction 1—session-1
Begin Tran
Update tblInventory set ItemsInStock = 9 where Id=1
-- Billing the customer
Waitfor Delay '00:00:15'
-- Insufficient Funds. Rollback transaction
Rollback Transaction
--Transaction 2-session 2
Set Transaction Isolation Level Read Uncommitted
Select * from tblInventory where Id=1
Or
Select * from tblInventory (NOLOck)
where Id=1
Task#2
1. Show and implement dirty data example (Use any other example)
2. Apply appropriate isolation level to prevent the dirty
Isolation levels
Isolation level is required to isolate a resource and protect it from other transactions. This is achieved with
the help of locks but what locks are needed and how they can be established is decided on the isolation
level set on the database level. If low level of Isolation is set, it allows multiple users to access the
resources concurrently but it may result in many concurrency related problems like phantom reads, dirty
reads etc. If higher levels of Isolation is set then it eliminate the concurrency related problem but it results
in less number of concurrent access and it may result in data blocking
There are the five Isolation levels (from lower level to higher level) defined in the SQL Server.
Read Uncommitted
Read Committed
Repeatable Read
Serializable
Snapshot