Bài giảng Kiến trúc cài đặt cơ sở dữ liệu - Chương 8 cung cấp cho người học kiến thức cơ bản về Transacion và bài toán đông thời. Nội dung trình bày trong chương này gồm có: Transacion là gì? Tính chất của Transaction, thực thi transacion tường minh, bài toán đông thời và cơ chế quản lý khóa. Mời các bạn tham khảo.
Ch GV Phi Loan - FIT - UIH ng N i dung • • • • T T ̀ T ̀ B ̀ –C ̀ –C ̀ –G ̀ GV Phi Loan - FIT - UIH ̀ ̀ ? ̀ a Transaction ̀ ̀ ng minh ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ a ̀ ̀ ̀ ̀ a a ̀ ̀ a Khái quát v Transaction • A transaction is a sequence of operations performed as a single logical unit of work • N ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀N ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀C“DL̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ GV Phi Loan - FIT - UIH B transaction ACID a • T́nh nguyên t (Atomicity): m t transaction ph i đ n v công vi c nguyên t ; ho c t t c s a đ i d li u đ u đ c th c thi ho c không s a đ i đ c th c thi • T́nh nh t qún (Consistency): m t giao tác ph i chuy n CSDL t tr ng thái nh t quán sang tr ng thái nh t quán khác • T́nh cô l p (Isolation) : Nh ng ch nh s a đ c làm b i transaction hi n hành ph i đ c cô l p kh i nh ng ch nh s a đ c làm b i transaction hi n hành khác • T́nh b n v ng (Durability): nh ng thay đ i c a CSDL giao tác th c hi n thành công b n v ng, không b m t k c ć l i x y sau đ́ GV Phi Loan - FIT - UIH P i Transaction • Giao t́c t ng minh (explicit transaction): đ c khai báo b ng l nh BEGIN TRANSACTION • Giao t́c ng m đ nh (implicit transaction): giao tác m i s t đ ng b t đ u giao tác tr c đ́ hoàn t t, nh ng m i ̀ ̀ ̀ ̀ ̀ ̀ ̀ nh COMMIT hay ROLLBACK • Giao t́c t đ ng chuy n giao (autocommit transaction): m i m t l nh đ GV Phi Loan - FIT - UIH c xem nh transaction Transaction SQL server • Đ hồn thành u c u c a tính ch t ACID trên, SQL Server cung c p ch ̀ sau: – Qu n lý Transaction (Transaction management) –Q ̀ ̀Khoá (Locking Management) – Ghi nh t ký (Logging) GV Phi Loan - FIT - UIH ̀ QUAN Ĺ TRANSACTION GV Phi Loan - FIT - UIH Đ nh transaction ng minh • BEGIN TRAN[SACTION] [transaction_name] Dùng đ đánh d u vi c b t đ u c a transaction • COMMIT [TRAN[SACTION] [transaction_name] Hay COMMIT WORK Dùng đ đánh d u vi c k t thúc c a transaction t ng minh GV Phi Loan - FIT - UIH Ví d GV Phi Loan - FIT - UIH Làm th quay v l i tr c nh ng i ROLLBACK [TRAN[SACTION] [transaction_name |savepoint_name ] D ̀ quay ng c m t transaction t ng minh hay ng ̀ nh v l ̀ m b ̀ u, hay v m d ng (save-point) bên transaction GV Phi Loan - FIT - UIH 10 C a • M ̀ ̀ ̀ ̀ ̀ ̀̀ ̀ ̀ ̀̀QL̀̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀̀T ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ c commit, “QL̀“ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ c commit GV Phi Loan - FIT - UIH ̀ ̀ 50 Ví d v s d ng khố • User1 th c hi n l nh sau đ c p nh t m ngày thi cho ng viên ć mã ‘000002’ b ng ExternalCandidate BEGIN TRANSACTION UPDATE ExternalCandidate SET siTestScore = 90 WHERE cCandidateCode='000002' UPDATE ExternalCandidate SET dTestDate = getdate() WHERE cCandidateCode = '000002' GV Phi Loan - FIT - UIH 51 Ví d v s d ng khố • T ̀ ̀ ̀ ̀ ̀ c hi n, User2 mu n l p l ch ph ng v n cho ng viên, nh ng không th xem chi ti t c a ̀ ̀ ̀ m thi 80 User2 ̀̀ d ng l nh sau : BEGIN TRANSACTION SELECT * from ExternalCandidate WHERE siTestScore > 80 UPDATE ExternalCandidate SET dInterviewDate = getdate()+ WHERE siTestScore > 80 H ̀ ̀ nh t i user2 không th th c thi transaction GV Phi Loan - FIT - UIH 52 Ví d v s d ng khố • Các b ng s b khoá transaction máy th c hi n • Khi transaction máy k t thúc b ng cách dùng l nh sau: COMMIT TRANSACTION Thì transaction máy m i đ GV Phi Loan - FIT - UIH c th c hi n 53 L nh DBCC USEROPTIONS statement • Đ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ nh DBCC USEROPTIONS USE AdventureWorks; GO SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; GO DBCC USEROPTIONS; GO GV Phi Loan - FIT - UIH 54 P p • K ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀DML̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ y • M ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ i GV Phi Loan - FIT - UIH 55 L p SET TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SNAPSHOT | SERIALIZABLE } [;] GV Phi Loan - FIT - UIH V SET TRANSACTION ISOLATION LEVEL REPEATABLE READ GO BEGIN TRANSACTION SELECT * FROM publishers SELECT * FROM authors COMMIT TRANSACTION GV Phi Loan - FIT - UIH 57 N a - Deadlock • A deadlock occurs when two or more tasks permanently block each other because each task has a lock on a resource that the other task(s) are trying to lock • D ̀ ̀ ̀ ̀ ̀deadly embrace GV Phi Loan - FIT - UIH 58 Deadlocks • K ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀CTUD̀ ̀DBM“̀ a ̀ ̀ ̀ ̀ ̀ ̀ ̀ –C ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ c –N ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀DBM“̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ a GV Phi Loan - FIT - UIH 59 P • • • • i deadlock Conversion Writer-writer ‘ ̀ Cascading GV Phi Loan - FIT - UIH ̀ ̀ t) 60 P i deadlock • Conversion deadlocks̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ nh • Writer-writer deadlocks ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ • Reader-writer deadlocks ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ i GV Phi Loan - FIT - UIH 61 P i deadlock • Cascading deadlocks ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ n deadlock –N ̀“QL̀“ ̀ ̀ ̀“QL̀“ ̀ ̀ ̀ ̀ ̀ ̀ ̀ t GV Phi Loan - FIT - UIH ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ 62 C t deadlock • “QL̀“ ̀ ̀ ̀ ̀ deadlock • M ̀ ̀ ̀ ̀ ̀ ̀N ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ i 100 milliseconds GV Phi Loan - FIT - UIH ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ m ̀ ̀ ̀ ̀ ̀ ̀ ̀ 63 C t deadlock • K ̀ ̀ ̀ ̀ rollback –P ̀ victim –D ̀ • C ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀deadlock ̀ ̀ ̀ t ̀ ̀ ̀ ̀ ̀ ̀DÉDLOCK P‘IO‘ITỲ ̀ n GV Phi Loan - FIT - UIH ̀ ̀ ̀ ̀ ̀ ̀ ̀t ̀ ̀ ̀ ̀ ̀ ̀ 64 ... ̀ ̀ ̀ ̀ ̀ c GV Phi Loan - FIT - UIH 29 GV Phi Loan - FIT - UIH 30 Intent locks •B “ ̀ ̀ ̀ ̀ a ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀T ̀ ̀ ̀ ̀ ̀“QL̀“ ̀ ̀ ̀ ̀ ̀T ̀ ̀ u GV Phi Loan - FIT - UIH ̀“QL̀ ̀ ̀ ̀ ̀ ̀ ̀... GV Phi Loan - FIT - UIH 43 C a SQL server DB Engine “ŃP“HOT ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ ̀ u transaction SERIALIZABLE GV Phi Loan - FIT - UIH ̀ ̀ ̀ ̀ ̀ ̀ ̀ 44 C i GV Phi Loan - FIT - UIH 45 Nonstandard... ̀ ̀11; COMMIT TRANSACTION; GV Phi Loan - FIT - UIH 15 V i END TRY BEGIN CATCH; ROLLBACK TRANSACTION; ‘́I“E‘‘O‘ I ̀T RETURN; END CATCH; GV Phi Loan - FIT - UIH ̀E ̀16, 1); 16 H m XACT_STATE() •