mysql浜嬪姟闅旂绾у埆/鑴忚/涓嶅彲閲嶅璇?骞昏璇﹁В
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql浜嬪姟闅旂绾у埆/鑴忚/涓嶅彲閲嶅璇?骞昏璇﹁В相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/mys' title='mys'>mys
澶氭 娌℃湁 鍙樻洿 鍏跺畠 ble mysq 璁茶В cti涓€銆佸洓绉嶄簨鍔¢殧绂荤骇鍒?/span>
1.1 read uncommitted 璇绘湭鎻愪氦
鍗筹細浜嬪姟A鍙互璇诲彇鍒颁簨鍔宸?span style="color: #0000ff;">淇敼浣嗘湭鎻愪氦鐨勬暟鎹€?/p>
闄ら潪鏄枃绔犻槄璇婚噺锛屾瘡娆?1杩欑鏃犲叧鐥涚棐鐨勫満鏅紝涓€鑸笟鍔$郴缁熸病鏈変汉浼氫娇鐢ㄨ浜嬪姟闅旂绾у埆锛屾爣鍑嗗疄鍦ㄥお瀹芥澗浜嗐€?/p>
1.2 read committed 璇诲凡鎻愪氦(绠€绉癛C)
鍗筹細浜嬪姟A鍙兘璇诲彇鍒颁簨鍔淇敼骞跺凡鎻愪氦鐨勬暟鎹€?/p>
杩欎釜绾у埆鐩稿瑕佷弗鏍间竴浜涳紝鑷冲皯鏄绛夊叾瀹冧簨鍔℃妸鍙樻洿鎻愪氦鍒癲b锛屾墠鑳借鍙栧埌锛屽惉涓婂幓铔潬璋辩殑銆備絾鏄湁浜涗笟鍔″満鏅紝姣斿浼氬憳绯荤粺涓紝濡傛灉瑕佸湪涓€涓簨鍔′腑锛屽娆¤鍙栫敤鎴疯韩浠斤紝鍒ゆ柇鏄惁浼氬憳锛屽鏋滃垰寮€濮嬭鍙栧埌璇ョ敤鎴锋槸浼氬憳锛屽仛浜嗕竴浜涢€昏緫澶勭悊锛屽悗闈㈠張璇诲埌鐢ㄦ埛涓嶆槸浼氬憳浜嗭紝杩欏氨鏈夌偣宕╂簝锛屼笉鐭ラ亾濡備綍缁х画銆傝繖绉嶅笇鏈涘悓1涓簨鍔′腑锛屽叧閿暟鎹笉绠¤鍙栧娆℃锛岀粨鏋滈兘涓€鏍凤紝RC绾у埆灏变笉琛屼簡銆?/p>
1.3 repeatable read 鍙噸澶嶈
鍗筹細鍚屼竴涓簨鍔′腑锛屽娆¤鍙栨煇涓€琛岃褰曪紝濮嬬粓鏄竴鏍风殑鍊硷紝涓嶇鍦ㄦ鏈熼棿锛屽叾瀹冧簨鍔℃湁娌℃湁淇敼杩囪鏁版嵁(涓嶈鏄惁鎻愪氦)銆傝绾у埆瑙e喅浜哛C涓嶅彲閲嶅璇荤殑闂锛屼絾鏄瓨鍦ㄥ够璇婚棶棰橈紙骞昏鍚庨潰浼氳瑙o級銆?/p>
1.4 serializable 涓茶鍖?/p>
鍗筹細涓€涓簨鍔″湪淇敼鍏跺畠鏁版嵁鏃讹紝濡傛灉鏈夊叾瀹冧簨鍔′篃鎯虫敼锛屽繀椤荤瓑鍓嶉潰鐨勪簨鍔℃彁浜ゆ垨鍥炴粴鍚庯紝鎵嶈兘缁х画銆傛渶涓ユ牸鐨勭骇鍒紝浣嗘槸鎬ц兘鏈€浣庯紝涔熷嚑涔庢病浜虹敤銆?/p>
浜屻€佽剰璇?涓嶅彲閲嶅璇?骞昏
2.1 鑴忚
楠岃瘉锛?/p>
a. 鎵句竴涓猰ysql鐜锛屽缓涓€涓祴璇曡〃t_people锛屽氨2鍒?id 锛宯ame
b. 寮€浜屼釜mysql缁堢锛岃繛鍒癲b涓婏紝涓烘柟渚胯瑙o紝杩?涓粓绔О涓?ldquo;缁堢1”銆?ldquo;缁堢2”锛岀粓绔?閲岃緭鍏ワ細
set session transaction isolation level read uncommitted; start transaction;
鍗筹細璁剧疆褰撳墠浼氳瘽鐨勯殧绂荤骇鍒负"璇绘湭鎻愪氦"銆?/p>
缁堢2閲岋紝杈撳叆:
start transaction; update t_people set name=鈥榵xx鈥?where id=1;
鐒跺悗鍐嶅洖鍒?ldquo;缁堢1”锛屾墽琛?nbsp;
select id,name from t_people where id=1;
鍙互鐪嬪埌锛岃鍙栧埌浜嗘湭鎻愪氦鐨勮剰鏁版嵁 銆?nbsp;缁堢2閲岋紝姝ゆ椂濡傛灉鎵цrollback鍥炴粴
缁堢1閲岋紝缁х画鎵ц
select id,name from t_people where id=1;
鍙互鍙戠幇鏈€鏂扮粨鏋滐紝宸茬粡鏄洖婊氬悗鐨勬暟鎹€傚緢鏄剧劧锛氬鏋滄湁鑴忚闂鍑虹幇锛屽氨鏇村姞淇濊瘉涓嶄簡“鍙噸澶嶈”銆?/p>
2.2 涓嶅彲閲嶅璇?/p>
灏嗕簨鍔¢殧绂荤骇鍒缃垚read committed锛堝嵆锛氳宸叉彁浜わ級锛屽彲瑙e喅鑴忚闂锛屼絾婊¤冻涓嶄簡“鍙噸澶嶈闇€姹?rdquo;銆?/p>
楠岃瘉鏂规硶璺熷垰鎵嶇被浼硷紝缁堢1閲岃緭鍏ワ細
set session transaction isolation level read committed;
灏嗙骇鍒缃垚RC锛岀劧鍚?涓粓绔噷閮藉紑鍚簨鍔★紝缁堢2涓紝淇敼涓€琛屾暟鎹紝浣嗘槸涓嶆彁浜わ紝姝ゆ椂缁堢1閲屽簲璇ユ槸璇讳笉鍒扮粓绔?淇敼鐨勬暟鎹€傜劧鍚庣粓绔?鎻愪氦锛岀粓绔?鎵嶈兘璇诲埌淇敼鍚庣殑鏁版嵁銆傜粓绔?濡傛灉缁х画淇敼銆佹彁浜わ紝缁堢1閲屽啀璇诲彇杩?琛岋紝灏嗘槸鏈€鏂扮殑鍊笺€傦紙涔熷氨鏄彧璇达紝鍙缁堢2涓嶆柇淇敼锛屼笉鏂彁浜わ紝缁堢1閲屽氨鑳借鍒拌繖琛屼笉鍚岀殑鏂板€硷紝鍗筹細淇濊瘉涓嶄簡鍚?涓簨鍔′腑锛屽悓涓€琛屾暟鎹紝澶氭閲嶅璇诲彇鐨勫€间笉鍙橈級
2.3 骞昏
灏嗛殧绂荤骇鍒户缁皟鏁磋嚦Repeatable Read锛岃繕鏄垰鎵嶇殑鍦烘櫙锛屽彉鎴愯繖鏍凤細
浜嬪姟A瀵逛簬鍚屼竴琛屾暟鎹紝涓嶇璇诲灏戞锛屽缁堟槸鐩稿悓鐨勫€硷紝瀹屽叏涓嶇悊浼氭湁娌℃湁鍏跺畠浜嬪姟鍦ㄤ慨鏀瑰畠銆傛湁鐐癸細“涓よ€充笉闂荤獥澶栦簨锛屼竴蹇冨彧璇诲湥璐や功”鐨勫懗閬撱€備絾鏄繖涔熸湁闂锛屾瘮濡傜鏉€璁㈠崟绯荤粺涓紝浜嬪姟A绗?娆¤鍙栧晢鍝佸簱瀛橈紝鍙戠幇杩樻湁1涓紝鍙互涓嬪崟锛岃刀绱х户缁紝浣嗘槸姝ゆ椂锛屽彲鑳芥湁鍙︿竴涓簨鍔★紝涔熷湪涓嬪崟锛屽凡缁忔彁浜や簡璁㈠崟锛屾妸搴撳瓨鍑忎负0浜嗭紝浜嬪姟A骞朵笉鐭ラ亾锛屽洜涓哄娆¤鍙栧簱瀛樼殑鍊兼槸涓€鏍风殑锛岃繕鏄?锛屾渶鍚庝粛鐒舵妸璁㈠崟鍒涘缓浜嗭紝褰㈡垚瓒呭崠銆?/p>
楠岃瘉鏂规硶锛?/p>
set session transaction isolation level repeatable read;
鍓╀笅鐨勬楠よ窡鍓嶉潰绫讳技锛屽氨涓嶉噸澶嶈禈杩颁簡銆傘€€
2.4 涓茶鍖?/p>
浠巇b灞傞潰锛岃鎯冲悓鏃惰В鍐宠剰璇汇€佷笉鍙噸澶嶈銆佸够璇伙紝鍙湁涓茶鍖栬繖涓骇鍒彲浠ュ仛鍒般€?/p>
set session transaction isolation level serializable;
濡備笅鍥撅細缁堢1璁剧疆涓茶鍖栧悗锛岀揣鎺ョ潃select xxx where id=1杩欐潯璇彞鍚庯紝id=1鐨勮繖琛岃褰曪紝灏辫閿佷簡銆?/p>
鍦ㄧ粓绔?閲岋紝鏇存柊鍏跺畠璁板綍锛堝嵆锛歩d涓嶇瓑浜?锛夊彲浠ユ甯告垚鍔燂紝浣嗘槸鏇存柊id=1 鏃讹紝灏变細鍗′綇锛岄櫎闈炵粓绔?鎶婁簨鍔℃彁浜ゆ垨鍥炴粴锛屽惁鍒欏皢涓€鐩村崱鐫€锛岀洿鍒拌秴鏃跺け璐ャ€?/p>
灏忕粨锛?/p>
闅旂绾у埆銆€銆€ | 瀛樺湪鐨勯棶棰?/td> |
璇绘湭鎻愪氦銆€銆€銆€ | 鑴忚銆佷笉鍙噸澶嶈銆佸够璇?/td> |
璇诲凡鎻愪氦銆€銆€ | 涓嶅彲閲嶅璇汇€佸够璇?/td> |
鍙噸澶嶈 | 骞昏 |
涓茶鍖?/td> | 鎬ц兘闂 |
闅旂绾у埆瓒婁弗鏍硷紝db缁煎悎鎬ц兘瓒婁綆銆?/p>
寤鸿锛?/span>
澶у鏁版儏鍐典笅锛孯C(璇诲凡鎻愪氦)鍩烘湰涓婂氨瓒冲浜嗭紝濡傛灉骞跺彂搴﹂珮锛屽彲浠ヨ€冭檻“RC绾у埆+(搴旂敤灞?鍒嗗竷寮忛攣”锛岃繖鏍峰嵆鑳戒繚璇佹暟鎹纭紝瀵筪b鐨勬€ц兘鍘嬪姏涔熻緝浣庛€?/p>
以上是关于mysql浜嬪姟闅旂绾у埆/鑴忚/涓嶅彲閲嶅璇?骞昏璇﹁В的主要内容,如果未能解决你的问题,请参考以下文章
mysql姝婚攣com.mysql.cj.jdbc.exception.MYSQLTransactionRollbackException Deadlock found when trying to