what is a transaction? a transaction is a logical logic of work a transaction may have one of two...
TRANSCRIPT
What is a Transaction?
A transaction is a logical logic of work
A transaction may have one of two outcomes– When a transaction completes successf
ully, it is “committed” or “saved”– When a transaction fails, it is “rolled ba
ck” or “undone”
Transaction management i n SQL Server
Protect data from software, hardwa re, or power failure
Provide access to multiple users to one or more database
Prevent simultaneous write and rea d of the same data by multiple users
Implemention TransactionControl Locking -Transaction control with Transact SQL
Error Management
What is locking? Locking is handled automatically by
SQL Server Data pages locked Locking ensures that transactions ex
ecuting simultaneously do not inter face with each other
- Without locking in a multi user syste m, you may get data inconsistency
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 p
age locks whenever possible– Table Locks Table Locks : More efficient when a whole
table is accessed• An UpdateUpdate with no, or a non selective WWherehere the server might esculate a tabl
e lock• Once statement accumulates 200 page
s locks SQL Server attemps to issue a ta ble lock
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 t ime to obtain a lock becomes smaller , b
ut degrade performance user must wa its until the lock are released
Concurrency Overhead - Trade off : You must balance overhead
and concurrency
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 lo
ck 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
Examples : Page Locks
Command Executed Command Executed Lock Acquired Lock Acquired Select?Select? Modify?Modify? Select title_id from titles ShareYes No Delete titles where Price>25ExclusiveNo No Insert titles value() ExclusiveNo No
Update titles Update, Yes No set type = “general” then ExclusiveNo
where type = “business”
Deadlock Two processes hold locks on a page
on which the other process needs a conflicting lock
SQL Server Detects the deadlock au tomatically and aborted one transac
tion whose accumulated the least a mount of CPU time
Implement Transaction Control LockingLocking -Transaction control with Transac SQL Error Management
- Transact SQL Transaction C ontrol Statement
Begin {transaction | tran | work} [tran_name]
Rollback {transaction | tran | work} [ tran_name or save_name]
commit {transaction | tran | work} [tran_name]
Writing Code for transactions Standardize the code used in trans
action processing in an applicaton Consider the following:– Set the transaction mode– Perform error checking– Return messages to the client
Transaction Modes Chained– requires an implicit beginning for transaction– requires and explicit end for a tra
nsaction with rollbackrollback or commitcommit Unchained– requires an explicit BeginBegin TransactTransactionion– requires and explicit end for a tra
nsaction with rollbackrollback or commitcommit
Chained Transactions mode Execute an implicit begin tran delete , insert , open , fetch , select , and update
Requires an explicit rollbackrollback or comcom mit mit to end the transaction
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
Unchained Transactions mo de(default mode)
Requires begin transaction begin transaction statement
- To re enter unchained mode type SeSe t chained off t chained off
Check the global variable @@ tranc hained to determine if you are in un
chained mode Transaction control statements used
in unchained mode are: begin tran, save tran, commit tran, r begin tran, save tran, commit tran, r
ollbacktranollbacktran
declare @err int
begin tranbegin 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
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
Rolling back a transaction to a Savepoint
In unchained mode , set up a savep oint within a transaction
save {transaction | tran} savepoint_name
To undo all the statements or proced ures between the savepoint and th
e roolback
rollback {transaction | tran | work}savepoint_name
begin tranbegin tran
update acct_svc_chge
set serv_chge = serv_chge + $.25
save transaction chargesave transaction charge
update acct_savings
set balance = balance-100
where myacct = “97345”
if @@error = 2
begin
rollback transaction charge charge
return
end
.
.
Implement Transaction Control LockingLocking -Transaction control with Transac SQL -Transaction control with Transac SQL Error Management
Error processing for Transactions @@error@@error
detect errors during/after statement execution
@@transtate@@transtate monitors the current state of th e transaction– Reset after a data modification statement,inset,del
ete or update
Value Meaning 0 Transaction in progress 1 Transaction committed 2 The previous statement was aborted
and transaction still in progress 3
transaction aborted/statement rolled back
Handling Errors in a transaction Check for an error by If @@error !
0= If an error occures, execute the rollb
ack transaction and return commandbegin tran
insert sales ……..
If @@error != 0
begin
rollback transaction
return
end
Transaction and @@rowcount Affected by statement– Insert, update, delete– Select
If a statement is expected to retur n a rows and 0@@rowcount is– rollback– abort the transaction with return