What is a Transaction? A transaction is a logical logic of work A transaction may have one of two...

25

Transcript of What is a Transaction? A transaction is a logical logic of work A transaction may have one of two...

Page 1: 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,
Page 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,

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”

Page 3: 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,

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

Page 4: 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,

Implemention TransactionControl Locking -Transaction control with Transact SQL

Error Management

Page 5: 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,

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

Page 6: 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,

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

Page 7: 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,

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

Page 8: 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,

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

Page 9: 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,

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”

Page 10: 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,

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

Page 11: 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,

Implement Transaction Control LockingLocking -Transaction control with Transac SQL Error Management

Page 12: 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,

- 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]

Page 13: 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,

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

Page 14: 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,

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

Page 15: 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,

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

Page 16: 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,

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

Page 17: 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,

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

Page 18: 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,

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

Page 19: 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,

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

Page 20: 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,

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

Page 21: 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,

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

.

.

Page 22: 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,

Implement Transaction Control LockingLocking -Transaction control with Transac SQL -Transaction control with Transac SQL Error Management

Page 23: 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,

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

Page 24: 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,

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

Page 25: 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,

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