Download presentation
Presentation is loading. Please wait.
Published byMargaret Logan Modified over 9 years ago
2
What is a Transaction? A transaction is a logical logic of work A transaction may have one of two outcomes –When a transaction completes successfully, it is “committed” or “saved” –When a transaction fails, it is “rolled back” or “undone”
3
Transaction management in SQL Server Protect data from software, hardware, or power failure Provide access to multiple users to one or more database Prevent simultaneous write and read of the same data by multiple users
4
Implemention Transaction Control Locking Transaction control with Transact-SQL Error Management
5
What is locking? Locking is handled automatically by SQL Server Data pages locked Locking ensures that transactions executing simultaneously do not interface with each other Without locking in a multi-user system, you may get data inconsistency
6
Locking in SQL Server SQL Server handles all locking decision SQL server has two primary level of locking: –Page Locks –Page Locks : SQL Server attemps to use page locks whenever possible –Table Locks –Table Locks : More efficient when a whole table is accessed Update WhereAn Update with no, or a non selective Where the server might esculate a table lock Once statement accumulates 200 pages locks SQL Server attemps to issue a table lock
7
Granularity of Locks Refer to how much of the data is locked at one time. SQL Server can lock as little as a page of data or an entire database Increasing the amoung of data at one time to obtain a lock becomes smaller, but degrade performance user must waits until the lock are released Concurrency Overhead Trade-off : You must balance overhead and concurrency
8
Type of page locks Shared lock (S) –Multiple transaction can lock a share page –No transaction can change the page Exclusive lock (X) –Only one transaction can lock the page –Other transaction wait until the exclusive lock release Update lock (U) –Allows reads, but will not allow U of X lock –Become X lock when the page is ready to be modified –Help as void Deadlock
9
Examples : Page Locks Command ExecutedLock AcquiredSelect?Modify? Select title_id from titlesShare YesNo Delete titles where Price>25ExclusiveNo No Insert titles value()ExclusiveNo No Update titles Update,YesNo set type = “general”then ExclusiveNo where type = “business”
10
Deadlock Two processes hold locks on a page on which the other process needs a conflicting lock SQL Server Detects the deadlock automatically and aborted one transaction whose accumulated the least amount of CPU time
11
Implement Transaction Control Locking Locking Transaction control with Transac-SQL Error Management
12
Transact-SQL Transaction Control Statement Begin {transaction | tran | work} [tran_name] Rollback {transaction | tran | work} [tran_name or save_name] commit {transaction | tran | work} [tran_name]
13
Writing Code for transactions Standardize the code used in transaction processing in an applicaton Consider the following: –Set the transaction mode –Perform error checking –Return messages to the client
14
Transaction Modes Chained –requires an implicit beginning for transaction rollbackcommit –requires and explicit end for a transaction with rollback or commit Unchained Begin Transaction –requires an explicit Begin Transaction rollbackcommit –requires and explicit end for a transaction with rollback or commit
15
Chained Transactions mode Execute an implicit begin tran delete, insert, open, fetch, select, and update rollbackcommit Requires an explicit rollback or commit to end the transaction
16
If @@tranchained = 0 set chained on declare @err int delete sales where stor_id = “5023” and ord_num = “AB-123-DEF” select @err = @@error if @err != 0 begin rollback work return end delete salesdetail where stor_id = “5023” where ord_num = “AB-123-DEF” select @err = @@error if @err != 0 begin rollback work return end commit work go
17
Unchained Transactions mode(default mode) begin transaction Requires begin transaction statement Set chained off To re-enter unchained mode type Set chained off Check the global variable @@ tranchained to determine if you are in unchained mode begin tran, save tran, commit tran, rollbacktran Transaction control statements used in unchained mode are: begin tran, save tran, commit tran, rollbacktran
18
declare @err int begin tran delete sales where stor_id = “5023” and ord_num = “AB-123-DEF” select @err = @@error if @err != 0 begin rollback tran return end delete salesdetail where stor_id = “5023” where ord_num = “AB-123-DEF” select @err = @@error if @err != 0 begin rollback tran return end commit tran go
19
Rolling back a transaction Prior to commit, trnasaction can be –Partially rolled back to a named savepoint –entirely rolled back After commit, a transaction cannot be rolled back
20
Rolling back a transaction to a Savepoint In unchained mode, set up a savepoint within a transaction save {transaction | tran} savepoint_name To undo all the statements or procedures between the savepoint and the roolback rollback {transaction | tran | work} savepoint_name
21
begin tran update acct_svc_chge set serv_chge = serv_chge + $.25 save transaction charge update acct_savings set balance = balance-100 where myacct = “97345” if @@error = 2 begin charge rollback transaction charge return end.
22
Implement Transaction Control Locking Locking Transaction control with Transac-SQL Transaction control with Transac-SQL Error Management
23
Error processing for Transactions @@error @@errordetect errors during/after statement execution @@transtate @@transtate monitors the current state of the transaction –Reset after a data modification statement,inset,delete or update ValueMeaning 0Transaction in progress 1Transaction committed 2The previous statement was aborted and transaction still in progress 3transaction aborted/statement rolled back
24
Handling Errors in a transaction Check for an error by If @@error != 0 If an error occures, execute the rollback transaction and return command begin tran insert sales …….. If @@error != 0 begin rollback transaction return end
25
Transaction and @@rowcount Affected by statement –Insert, update, delete –Select If a statement is expected to return a rows and @@rowcount is 0 –rollback –abort the transaction with return
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.