




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、數(shù)據(jù)庫原理實驗報告題目:實驗七事務(wù)與并發(fā)控制學(xué)號姓名班級日期2013302478紀昌宇100113012015.11.141. 實驗七:事務(wù)與并發(fā)控制1.1. 實驗?zāi)康?. 掌握事務(wù)機制,學(xué)會創(chuàng)建事務(wù)。2. 理解事務(wù)并發(fā)操作所可能導(dǎo)致的數(shù)據(jù)不一致性問題,用實驗展現(xiàn)四種數(shù)據(jù)不一致性問題:丟失修改、讀臟數(shù)據(jù)、不可重復(fù)讀以及幻讀現(xiàn)象。3. 理解鎖機制,學(xué)會采用鎖與事務(wù)隔離級別解決數(shù)據(jù)不一致的問題。4. 了解數(shù)據(jù)庫的事務(wù)日志。1.2. 實驗內(nèi)容 假設(shè)學(xué)校允許學(xué)生將銀行卡和校園卡進行綁定,在student數(shù)據(jù)庫中有如下的基本表,其中校園卡編號cardid即為學(xué)生的學(xué)號: icbc_card(studca
2、rdid,icbcid,balance) /校園卡ID,工行卡ID,銀行卡余額 campus_card(studcardid,balance) /校園卡ID,校園卡余額數(shù)據(jù)創(chuàng)建的代碼: use studentcreate table campus_card( studcardid Char(8) , balance Decimal(10,2)create table icbc_card( studcardid Char(8), icbcid Char(10), balance Decimal(10,2) ) insert into campus_card values('2015003
3、1', 30)insert into campus_card values('20150032', 50)insert into campus_card values('20150033', 70)insert into icbc_card values('20150031', '2015003101', 1000)insert into icbc_card values('20150032', '2015003201', 1000)insert into icbc_card values(
4、'20150033', '2015003301', 1000) 針對以上數(shù)據(jù)庫按照要求完成下列實驗:1. 編寫一個事務(wù)處理(begin tran)實現(xiàn)如下的操作:某學(xué)號為20150032的學(xué)生要從銀行卡中轉(zhuǎn)賬200元到校園卡中,若中間出現(xiàn)故障則進行rollback。(15分)set transaction isolation levelrepeatable readbegin transactionuse studentgodeclare x decimal(10, 2)select x=balance from icbc_cardwhere studcardi
5、d='20150032'set x=x-200 if(x>=0) begin update icbc_card set balance=x where studcardid='20150032' update campus_card set balance=balance+200 where studcardid='20150032' commit tran end else begin print '余額不足,不能轉(zhuǎn)賬' rollback tran end2. 針對本題的數(shù)據(jù)庫和表,分別用具體的例子展現(xiàn)四種數(shù)據(jù)不一致問題:
6、丟失修改、讀臟數(shù)據(jù)、不可重復(fù)讀和幻讀(刪除和插入)。(40分,每種數(shù)據(jù)不一致10分)(1).丟失修改-事務(wù)1begin trandeclare balance decimal(10,2)select balance=balance from campus_card where studcardid='20150033'waitfor delay '00:00:05'set balance=balance-10update campus_card set balance=balance where studcardid='20150033'comm
7、it trangoselect balance from campus_card where studcardid='20150033'-事務(wù)2begin trandeclare balance1 decimal(10,2)select balance1=balance from campus_card where studcardid='20150033'waitfor delay '00:00:05'set balance1=balance1-20update campus_card set balance=balance1 where st
8、udcardid='20150033'commit trangoselect balance from campus_card where studca 【事務(wù)1】更改了數(shù)據(jù),結(jié)果為60,但是沒有被讀到。最終【事務(wù)2】的結(jié)果50覆蓋了【事務(wù)1】的更改值,結(jié)果不是期望值40(2).讀臟數(shù)據(jù)-事務(wù)1 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED -READ UNCOMMITTED 執(zhí)行臟讀,不發(fā)出共享鎖,也不接受排他鎖begin tran declare balance decimal(10,2) select balance=b
9、alance from campus_card where studcardid='20150032' update campus_card set balance=balance+100 where studcardid='20150032' waitfor delay '00:00:05' rollback tran -回滾 goselect balance from campus_card where studcardid='20150032' -事務(wù)2 SET TRANSACTION ISOLATION LEVEL REA
10、D UNCOMMITTED begin tran declare balance decimal(10,2) select balance=balance from campus_card where studcardid='20150032' update campus_card set balance=balance+50 where studcardid='20150032' commit tran goselect balance from campus_card where studcardid='20150032' 【事務(wù)1】更改了數(shù)
11、據(jù),【事務(wù)2】讀取了表中更改后的值再進行操作,【事務(wù)1】回滾。最終的表存儲了錯誤結(jié)果。 (3)不可重復(fù)讀 -事務(wù)1 SET TRANSACTION ISOLATION LEVEL READ COMMITTED begin tran select balance from campus_card where studcardid='20150031' waitfor delay '00:00:05' select balance from campus_card where studcardid='20150031'commit tran -事務(wù)2
12、 SET TRANSACTION ISOLATION LEVEL READ COMMITTED begin tran update campus_card set balance=balance+200 where studcardid='20150031' commit tran 【事務(wù)1】讀取了數(shù)據(jù),【事務(wù)2】更改數(shù)據(jù),【事務(wù)1】再讀取數(shù)據(jù)?!臼聞?wù)1】兩次讀取都不一樣?。?).幻讀 插入 -事務(wù)1 SET TRANSACTION ISOLATION LEVEL READ COMMITTED begin tran select balance from campus_card
13、 where studcardid='20150031' waitfor delay '00:00:05' select balance from campus_card where studcardid='20150031'commit tran -事務(wù)2 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE begin tran insert into campus_card values('20150031', 30) commit tran 刪除-事務(wù)1 SET TRANSACTION
14、ISOLATION LEVEL READ COMMITTED begin tran select balance from campus_card where studcardid='20150031' waitfor delay '00:00:05' select balance from campus_card where studcardid='20150031'commit tran -事務(wù)2 set tran isolation level repeatable read begin tran delete from campus_ca
15、rd where studcardid='20150031'commit tran 3. 利用鎖機制、數(shù)據(jù)庫的隔離級別等,設(shè)計方案分別解決上述丟失修改、讀臟數(shù)據(jù)和不可重復(fù)讀(或者幻讀)的數(shù)據(jù)不一致問題。(30分,每種數(shù)據(jù)不一致10分,提示可以用sp_lock系統(tǒng)存儲過程查看當前鎖狀況)修改隔離級別以卻確定數(shù)據(jù)的正確性:丟失修改,在SQL語句前加未提交讀:set tran isolation level read uncommitted讀臟數(shù)據(jù),在SQL語句前已提交讀:set tran isolation level read committed不可重復(fù)讀,在SQL語句前可重復(fù)讀:set tran isolation level repeatable read幻讀在SQL語句前加可串行讀:set tran isolation level serializable4. 構(gòu)造一個出現(xiàn)死鎖的情形。(10分) 首先將鎖的級別改為提交可讀set tran isolation level read committed-事務(wù)1begin trandeclare read intselect read=gradefrom scwhere sno='95003'waitfor delay '00:00:10'update s
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 南昌職業(yè)大學(xué)《中學(xué)體育教學(xué)專題案例分析》2023-2024學(xué)年第二學(xué)期期末試卷
- 咸陽購房合同范本
- 修路合同范本簡版
- 做賬實操-電子廠成本核算制度
- 2025年02月朝陽市事業(yè)單位2022年從朝陽市入伍2024年退伍高校畢業(yè)生退役士兵筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 商品銷品合同范本
- 家居用品店面租賃合同
- 課題開題報告:大學(xué)招生專業(yè)選考科目要求的科學(xué)決策與管理制度設(shè)計
- 課題開題報告:大學(xué)生就業(yè)創(chuàng)業(yè)服務(wù)地方經(jīng)濟社會發(fā)展路徑研究
- 課題開題報告:變革社會中高校教師的教育價值觀敘事探究
- DB32T 3699-2019 城市道路照明設(shè)施養(yǎng)護規(guī)程
- 自然辯證法概論課件:第四章馬克思主義科學(xué)技術(shù)社會論
- 2021版大象版四年級科學(xué)下冊12奇妙的植物教學(xué)課件
- 精雕JDPaint快捷鍵大全
- 山東建筑電氣與智能化疑難問題分析與解答
- 2022年鄭州衛(wèi)生健康職業(yè)學(xué)院單招英語模擬試題(附答案解析)
- Q∕GDW 10354-2020 智能電能表功能規(guī)范
- 土壤學(xué)習(xí)題與答案
- 國家自然科學(xué)基金(NSFC)申請書樣本
- 觀摩臺標準化建設(shè)方案
- 數(shù)字化影像與PACS教學(xué)大綱
評論
0/150
提交評論