Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -"— Presentation transcript:

1

2 Applied Database II Transactions In Database

3 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

4 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.”

5 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

6 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 to determine nesting level

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

8 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

9 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

10 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

11 Concurrency Problems Prevented by Locks Lost Update Uncommitted Dependency (Dirty Read) Inconsistent Analysis (Nonrepeatable Read) Phantoms Reads

12 Locks Types of locks Basic Locks Shared Exclusive Special Situation Locks Update Intent Schema Key-range

13 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

14 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

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

16 A Simple of Begin Transaction Structure Begin Transaction [ ] [Commit Transaction [ ] ] [Rollback Transaction [ ] ]

17 Example aktifkan database Inventory use Inventory ---- titik awal transaksi Begin Transaction ---- set format tanggal dd-mm-yy Set DateFormat dmy; ---- update tabel Beli insert into Beli values (’ANEKA’, ’001/05/05’, ’ ’, ’PS.001’, 24, 1200,); ---- pembelian mempengaruhi stok if exists(select * from Stok where KdBr=’Ps.001’) update Stok set Banyak= Banyak+24 else insert into Stok values (‘PS.001’, 24) ---- pembelian mempengaruhi Hutang

18 Cont’ if exists (select * from hutang where KdSpl=’ANEKA’ and NoFak=’001/05/05’ and TgFak=’ ’) Update Hutang Set jumlah=jumlah+(24*1200) Else insert into Hutang values (’ANEKA’, ’001/05/05’, ’ ’, ’PS.001’, (24* 1200)); ---- cek data jika jumlah hutang melebihi jumlah tertentu transaksi batal, ---- jika tidak transaksi disimpan permanen if (select sum(jumlah) from hutang where KdSpl=’ANEKA’) > begin raiserror( ’Hutang lebih dari 10 juta, transaksi dibatalkan!’,1,1); Rollback Transaction; End Else Commit Transaction; ---- cek tabel select * from Beli; select * from Stok; select * from Hutang;

19 A Transaction in SP ---- aktifkan database Inventory use Inventory; Create Procedure as as as as as as Money as ---- titik awal transaksi Begin Transaction --- set format tanggal dd-mm-yy set DateFormat dmy; --- update tabel Beli Insert Into Beli

20 --- pembelian mempengaruhi stok if exists (select * from Stok where update Stok set else insert into Stok --- pembelian mempengaruhi hutang if exists (select * from Hutang where and and update Hutang set else insert into --- cek data, jika jumlah hutang melebihi jumlah tertentu transaksi batal, --- jika tidak transaksi disimpan permanen if (select sum(jumlah) from Hutang where begin raiserror( ‘Hutang lebih dari 10 juta, transaksi dibatalkan’,1,1); Rollback Transaction; End Else Commit Transaction; ---- jalankan stored procedure tersebut execute = = = = = = 1200;


Download ppt "Applied Database II Transactions In Database The ACID Test Atomicity The whole transaction or none of it Consistency Remains in a consistent state -"

Similar presentations


Ads by Google