Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none...

19
Applied Database II Transactions In Database

Transcript of Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none...

Page 1: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Applied Database II

Transactions In Database

Page 2: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

The ACID Test

Atomicity• The whole transaction or none of it

Consistency• Remains in a consistent state - Integrity

Isolation• Insulated from other operations

Durability• Changes are permanent

Page 3: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Transactions

Ensure that the boundaries of the transaction are known

Three types• Auto commit – any statement that modifies

data

• Explicit – specifically declared

• Implicit – applies to a connection “A Transact-SQL batch is not a

transaction unless stated explicitly.”

Page 4: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Transactions

Transactions Ensure That Multiple Data Modifications Are Processed Together

Locks Prevent Update Conflicts• Transactions are serializable

• Locking is automatic

• Locks allow concurrent use of data

Concurrency Control

Page 5: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Considerations for Using Transactions

Transaction Guidelines• Keep transactions as short as possible

• Use caution with certain Transact-SQL statements

• Avoid transactions that require user interaction

Issues in Nesting Transactions• Allowed, but not recommended

• Use @@trancount to determine nesting level

Page 6: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Setting the Implicit Transactions Option

Automatically Starts a Transaction When You Execute Certain Statements

Nested Transactions Are Not Allowed Transaction Must Be Explicitly Completed

with COMMIT or ROLLBACK TRANSACTION

By Default, Setting Is Off

SET IMPLICIT_TRANSACTIONS ONSET IMPLICIT_TRANSACTIONS ON

Page 7: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Restrictions on User-defined Transactions

• ALTER DATABASE• BACKUP LOG• CREATE DATABASE• DROP DATABASE

• RECONFIGURE• RESTORE DATABASE• RESTORE LOG• UPDATE STATISTICS

Certain Statements May Not Be Included

Page 8: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Explicit Transactions

BEGIN TRAN• Marks the start of statements that must be

executed or aborted

COMMIT TRAN• Saves all changes to data

ROLLBACK TRAN• Reverses all changes to data

Page 9: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Concurrency Problems

Lost updates• Avoid by writing atomic UPDATE statements

Uncommitted dependency (“dirty read”)• Use READ COMMITTED isolation (default)

Inconsistent analysis (nonrepeatable read)• Use REPEATABLE READ isolation

Phantom reads• Use SERIALIZABLE isolation

Page 10: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Concurrency Problems Prevented by Locks

Lost Update Uncommitted Dependency (Dirty Read) Inconsistent Analysis (Nonrepeatable

Read) Phantoms Reads

Page 11: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Locks

Types of locks Basic Locks

• Shared

• Exclusive

Special Situation Locks• Update

• Intent

• Schema

• Key-range

Page 12: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Session-Level Locking Options

Transaction Isolation Level• READ COMMITTED (DEFAULT)

• READ UNCOMMITTED

• REPEATABLE READ

• SERIALIZABLE

Locking Timeout• Limits time waiting for a locked resource

• Use SET LOCK_TIMEOUT

Page 13: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Table-Level Locking Options

Use with Caution Can Specify One or More Locking Options

for a Table Use optimizer_hints Portion of FROM Clause

in SELECT or UPDATE Statement Overrides Session-Level Locking Options

Page 14: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Displaying Locking Information

Current Activity Window sp_lock System Stored Procedure SQL Profiler Windows 2000/XP System Monitor Additional Information

Page 15: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

A Simple of Begin Transaction Structure

Begin Transaction [<namaTransaction>]

.........

.........[Commit Transaction

[<namaTransaction>] ][Rollback Transaction

[<namaTransaction>] ]

Page 16: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Example

----- aktifkan database Inventoryuse Inventory---- titik awal transaksiBegin Transaction---- set format tanggal dd-mm-yySet DateFormat dmy;---- update tabel Beliinsert into Beli

values (’ANEKA’, ’001/05/05’, ’15-05-05’, ’PS.001’, 24, 1200,);---- pembelian mempengaruhi stokif exists(select * from Stok where KdBr=’Ps.001’)

update Stokset Banyak= Banyak+24

elseinsert into Stok

values (‘PS.001’, 24)---- pembelian mempengaruhi Hutang

Page 17: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Cont’

if exists (select * from hutang where KdSpl=’ANEKA’ and NoFak=’001/05/05’ and TgFak=’15-05-05’)

Update HutangSet jumlah=jumlah+(24*1200)

Else insert into Hutang

values (’ANEKA’, ’001/05/05’, ’15-05-05’, ’PS.001’, (24* 1200));---- cek data jika jumlah hutang melebihi jumlah tertentu transaksi batal,---- jika tidak transaksi disimpan permanenif (select sum(jumlah) from hutang where KdSpl=’ANEKA’) > 10000000begin

raiserror(’Hutang lebih dari 10 juta, transaksi dibatalkan!’,1,1);

Rollback Transaction;EndElse

Commit Transaction;---- cek tabelselect * from Beli;select * from Stok;select * from Hutang;

Page 18: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

A Transaction in SP

---- aktifkan database Inventoryuse Inventory;Create Procedure InBeli

@pKdSpl as Varchar(10),@pNoFak as Varchar(20),@pSTgFak as Varchar(10),@pKdBr as Varchar(10),@pBanyak as Money,@pHargaBeli as Money

as---- titik awal transaksiBegin Transaction--- set format tanggal dd-mm-yyset DateFormat dmy;--- update tabel BeliInsert Into Beli

Page 19: Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -

Values (@pKdSpl, @pNoFak, @pSTgFak, @pKdBr, @pBanyak, @pHargaBeli);--- pembelian mempengaruhi stokif exists (select * from Stok where KdBr=@pKdBr)

update Stokset Banyak=Banyak+@pBanyak

elseinsert into Stok

values (@pKdBr, @pBanyak);--- pembelian mempengaruhi hutangif exists (select * from Hutang where KdSpl=@pKdSpl and NoFak=@pNoFak and TgFak=@pSTgFak)

update Hutangset Jumlah=Jumlah+(@pBanyak*@pHargaBeli)

elseinsert into Hutang

values (@pKdSpl, @pNoFak, @pSTgFak, @pBanyak*@pHargaBeli));--- cek data, jika jumlah hutang melebihi jumlah tertentu transaksi batal,--- jika tidak transaksi disimpan permanenif (select sum(jumlah) from Hutang where begin

raiserror(‘Hutang lebih dari 10 juta, transaksi dibatalkan’,1,1);

Rollback Transaction;EndElse

Commit Transaction;---- jalankan stored procedure tersebutexecute InBeli

@pKdSpl = ‘ANEKA’,@pNoFak = ‘002/05/05’,@pSTgFak = ’16-05-05’,@pKdBr = ‘PS.002’,@pBanyak = 48,@pHargaBeli = 1200;