MyException - 我的异常网
当前位置:我的异常网» 行业应用 » 浜嬬墿鐨勪紶鎾瓥鐣?鎺у埗鍥炴粴鐨勭矑搴?浜嬬墿

浜嬬墿鐨勪紶鎾瓥鐣?鎺у埗鍥炴粴鐨勭矑搴?浜嬬墿鐨勯殧绂绘帶鍒朵簨鐗╃殑骞跺彂-绫讳技lock

www.MyException.Cn  网友分享于:2018-05-16  浏览:0次
浜嬬墿鐨勪紶鎾瓥鐣?-鎺у埗鍥炴粴鐨勭矑搴?浜嬬墿鐨勯殧绂绘帶鍒朵簨鐗╃殑骞跺彂---绫讳技lock

浜嬪姟闅旂绾у埆

浜嬬墿鐨勯殧绂绘帶鍒朵簨鐗╃殑骞跺彂---绫讳技lock

闅旂绾у埆鏄寚鑻ュ共涓苟鍙戠殑浜嬪姟涔嬮棿鐨勯殧绂荤▼搴︺€俆ransactionDefinition 鎺ュ彛涓畾涔変簡浜斾釜琛ㄧず闅旂绾у埆鐨勫父閲忥細

  • TransactionDefinition.ISOLATION_DEFAULT锛氳繖鏄粯璁ゅ€硷紝琛ㄧず浣跨敤搴曞眰鏁版嵁搴撶殑榛樿闅旂绾у埆銆傚澶ч儴鍒嗘暟鎹簱鑰岃█锛岄€氬父杩欏€煎氨鏄疶ransactionDefinition.ISOLATION_READ_COMMITTED銆?/li>
  • TransactionDefinition.ISOLATION_READ_UNCOMMITTED锛氳闅旂绾у埆琛ㄧず涓€涓簨鍔″彲浠ヨ鍙栧彟涓€涓簨鍔′慨鏀逛絾杩樻病鏈夋彁浜ょ殑鏁版嵁銆傝绾у埆涓嶈兘闃叉鑴忚锛屼笉鍙噸澶嶈鍜屽够璇伙紝鍥犳寰堝皯浣跨敤璇ラ殧绂荤骇鍒€傛瘮濡侾ostgreSQL瀹為檯涓婂苟娌℃湁姝ょ骇鍒€?/li>
  • TransactionDefinition.ISOLATION_READ_COMMITTED锛氳闅旂绾у埆琛ㄧず涓€涓簨鍔″彧鑳借鍙栧彟涓€涓簨鍔″凡缁忔彁浜ょ殑鏁版嵁銆傝绾у埆鍙互闃叉鑴忚锛岃繖涔熸槸澶у鏁版儏鍐典笅鐨勬帹鑽愬€笺€?/li>
  • TransactionDefinition.ISOLATION_REPEATABLE_READ锛氳闅旂绾у埆琛ㄧず涓€涓簨鍔″湪鏁翠釜杩囩▼涓彲浠ュ娆¢噸澶嶆墽琛屾煇涓煡璇紝骞朵笖姣忔杩斿洖鐨勮褰曢兘鐩稿悓銆傝绾у埆鍙互闃叉鑴忚鍜屼笉鍙噸澶嶈銆?/li>
  • TransactionDefinition.ISOLATION_SERIALIZABLE锛氭墍鏈夌殑浜嬪姟渚濇閫愪釜鎵ц锛岃繖鏍蜂簨鍔′箣闂村氨瀹屽叏涓嶅彲鑳戒骇鐢熷共鎵帮紝涔熷氨鏄锛岃绾у埆鍙互闃叉鑴忚銆佷笉鍙噸澶嶈浠ュ強骞昏銆備絾鏄繖灏嗕弗閲嶅奖鍝嶇▼搴忕殑鎬ц兘銆傞€氬父鎯呭喌涓嬩篃涓嶄細鐢ㄥ埌璇ョ骇鍒€?/li>

浜嬪姟浼犳挱琛屼负

浜嬬墿鐨勪紶鎾瓥鐣?-鎺у埗鍥炴粴鐨勭矑搴?/span>

鎵€璋撲簨鍔$殑浼犳挱琛屼负鏄寚锛屽鏋滃湪寮€濮嬪綋鍓嶄簨鍔′箣鍓嶏紝涓€涓簨鍔′笂涓嬫枃宸茬粡瀛樺湪锛屾鏃舵湁鑻ュ共閫夐」鍙互鎸囧畾涓€涓簨鍔℃€ф柟娉曠殑鎵ц琛屼负銆傚湪TransactionDefinition瀹氫箟涓寘鎷簡濡備笅鍑犱釜琛ㄧず浼犳挱琛屼负鐨勫父閲忥細

  • 浜嬬墿涔嬮棿缁戝湪涓€璧凤紝瑕佷箞涓€璧锋彁浜わ紝涓€璧峰洖婊毬?----PROPAGATION_REQUIRED锛岀敤浜庝弗鏍间竴鑷存€х殑瑕佹眰
  • 闅旂浜嬬墿锛屼簨鐗╀箣闂翠笉褰卞搷---TransactionDefinition.PROPAGATION_REQUIRES_NEW锛岀敤浜庢祦姘寸嚎涔嬮棿锛屼絾鍙堝悇鑷褰曠殑鍦烘櫙
  • 缁戝湪涓€璧风殑浜嬬墿涔嬮棿涓嶅奖鍝嶏紝涓嶄竴璧峰洖婊?---PROPAGATION_NESTED锛岀敤浜庢祦姘寸嚎涔嬮棿锛屼絾鍙堝悇鑷褰曠殑鍦烘櫙
  • TransactionDefinition.PROPAGATION_REQUIRED锛氬鏋滃綋鍓嶅瓨鍦ㄤ簨鍔★紝鍒欏姞鍏ヨ浜嬪姟锛涘鏋滃綋鍓嶆病鏈変簨鍔★紝鍒欏垱寤轰竴涓柊鐨勪簨鍔°€傝繖鏄粯璁ゅ€笺€?/span>
  • TransactionDefinition.PROPAGATION_SUPPORTS锛氬鏋滃綋鍓嶅瓨鍦ㄤ簨鍔★紝鍒欏姞鍏ヨ浜嬪姟锛涘鏋滃綋鍓嶆病鏈変簨鍔★紝鍒欎互闈炰簨鍔$殑鏂瑰紡缁х画杩愯銆?/span>
  • TransactionDefinition.PROPAGATION_MANDATORY锛氬鏋滃綋鍓嶅瓨鍦ㄤ簨鍔★紝鍒欏姞鍏ヨ浜嬪姟锛涘鏋滃綋鍓嶆病鏈変簨鍔★紝鍒欐姏鍑哄紓甯搞€?/span>
  • TransactionDefinition.PROPAGATION_REQUIRES_NEW锛氬垱寤轰竴涓柊鐨勪簨鍔★紝濡傛灉褰撳墠瀛樺湪浜嬪姟锛屽垯鎶婂綋鍓嶄簨鍔℃寕璧枫€?/span>
  • TransactionDefinition.PROPAGATION_NOT_SUPPORTED锛氫互闈炰簨鍔℃柟寮忚繍琛岋紝濡傛灉褰撳墠瀛樺湪浜嬪姟锛屽垯鎶婂綋鍓嶄簨鍔℃寕璧枫€?/span>
  • TransactionDefinition.PROPAGATION_NEVER锛氫互闈炰簨鍔℃柟寮忚繍琛岋紝濡傛灉褰撳墠瀛樺湪浜嬪姟锛屽垯鎶涘嚭寮傚父銆?/span>
  • TransactionDefinition.PROPAGATION_NESTED锛氬鏋滃綋鍓嶅瓨鍦ㄤ簨鍔★紝鍒欏垱寤轰竴涓簨鍔′綔涓哄綋鍓嶄簨鍔$殑宓屽浜嬪姟鏉ヨ繍琛岋紱濡傛灉褰撳墠娌℃湁浜嬪姟锛屽垯璇ュ彇鍊肩瓑浠蜂簬TransactionDefinition.PROPAGATION_REQUIRED銆?/span>

浜嬪姟瓒呮椂

鎵€璋撲簨鍔¤秴鏃讹紝灏辨槸鎸囦竴涓簨鍔℃墍鍏佽鎵ц鐨勬渶闀挎椂闂达紝濡傛灉瓒呰繃璇ユ椂闂撮檺鍒朵絾浜嬪姟杩樻病鏈夊畬鎴愶紝鍒欒嚜鍔ㄥ洖婊氫簨鍔°€傚湪 TransactionDefinition 涓互 int 鐨勫€兼潵琛ㄧず瓒呮椂鏃堕棿锛屽叾鍗曚綅鏄銆?/p>

榛樿璁剧疆涓哄簳灞備簨鍔$郴缁熺殑瓒呮椂鍊硷紝濡傛灉搴曞眰鏁版嵁搴撲簨鍔$郴缁熸病鏈夎缃秴鏃跺€硷紝閭d箞灏辨槸none锛屾病鏈夎秴鏃堕檺鍒躲€?/p>

浜嬪姟鍙灞炴€?/span>

鍙浜嬪姟鐢ㄤ簬瀹㈡埛浠g爜鍙浣嗕笉淇敼鏁版嵁鐨勬儏褰紝鍙浜嬪姟鐢ㄤ簬鐗瑰畾鎯呮櫙涓嬬殑浼樺寲锛屾瘮濡備娇鐢℉ibernate鐨勬椂鍊欍€?br style="margin: 0px; padding: 0px;">榛樿涓鸿鍐欎簨鍔°€?/p>

spring浜嬪姟鍥炴粴瑙勫垯

鎸囩ずspring浜嬪姟绠$悊鍣ㄥ洖婊氫竴涓簨鍔$殑鎺ㄨ崘鏂规硶鏄湪褰撳墠浜嬪姟鐨勪笂涓嬫枃鍐呮姏鍑哄紓甯搞€俿pring浜嬪姟绠$悊鍣ㄤ細鎹曟崏浠讳綍鏈鐞嗙殑寮傚父锛岀劧鍚庝緷鎹鍒欏喅瀹氭槸鍚﹀洖婊氭姏鍑哄紓甯哥殑浜嬪姟銆?/p>

榛樿閰嶇疆涓嬶紝spring鍙湁鍦ㄦ姏鍑虹殑寮傚父涓鸿繍琛屾椂unchecked寮傚父鏃舵墠鍥炴粴璇ヤ簨鍔★紝涔熷氨鏄姏鍑虹殑寮傚父涓篟untimeException鐨勫瓙绫?Errors涔熶細瀵艰嚧浜嬪姟鍥炴粴)锛岃€屾姏鍑篶hecked寮傚父鍒欎笉浼氬鑷翠簨鍔″洖婊氥€?br style="margin: 0px; padding: 0px;">鍙互鏄庣‘鐨勯厤缃湪鎶涘嚭閭d簺寮傚父鏃跺洖婊氫簨鍔★紝鍖呮嫭checked寮傚父銆備篃鍙互鏄庣‘瀹氫箟閭d簺寮傚父鎶涘嚭鏃朵笉鍥炴粴浜嬪姟銆?/p>

杩樺彲浠ョ紪绋嬫€х殑閫氳繃setRollbackOnly()鏂规硶鏉ユ寚绀轰竴涓簨鍔″繀椤诲洖婊氾紝鍦ㄨ皟鐢ㄥ畬setRollbackOnly()鍚庝綘鎵€鑳芥墽琛岀殑鍞竴鎿嶄綔灏辨槸鍥炴粴銆?/p>

@Transactional娉ㄨВ

@Transactional灞炴€?/span>

聽 灞炴€?绫诲瀷 鎻忚堪
value String 鍙€夌殑闄愬畾鎻忚堪绗︼紝鎸囧畾浣跨敤鐨勪簨鍔$鐞嗗櫒
propagation enum: Propagation 鍙€夌殑浜嬪姟浼犳挱琛屼负璁剧疆
isolation enum: Isolation 鍙€夌殑浜嬪姟闅旂绾у埆璁剧疆
readOnly boolean 璇诲啓鎴栧彧璇讳簨鍔★紝榛樿璇诲啓
timeout int (in seconds granularity) 浜嬪姟瓒呮椂鏃堕棿璁剧疆
rollbackFor Class瀵硅薄鏁扮粍锛屽繀椤荤户鎵胯嚜Throwable 瀵艰嚧浜嬪姟鍥炴粴鐨勫紓甯哥被鏁扮粍
rollbackForClassName 绫诲悕鏁扮粍锛屽繀椤荤户鎵胯嚜Throwable 瀵艰嚧浜嬪姟鍥炴粴鐨勫紓甯哥被鍚嶅瓧鏁扮粍
noRollbackFor Class瀵硅薄鏁扮粍锛屽繀椤荤户鎵胯嚜Throwable 涓嶄細瀵艰嚧浜嬪姟鍥炴粴鐨勫紓甯哥被鏁扮粍
noRollbackForClassName 绫诲悕鏁扮粍锛屽繀椤荤户鎵胯嚜Throwable 涓嶄細瀵艰嚧浜嬪姟鍥炴粴鐨勫紓甯哥被鍚嶅瓧鏁扮粍

鐢ㄦ硶

@Transactional 鍙互浣滅敤浜庢帴鍙c€佹帴鍙f柟娉曘€佺被浠ュ強绫绘柟娉曚笂銆傚綋浣滅敤浜庣被涓婃椂锛岃绫荤殑鎵€鏈?public 鏂规硶灏嗛兘鍏锋湁璇ョ被鍨嬬殑浜嬪姟灞炴€э紝鍚屾椂锛屾垜浠篃鍙互鍦ㄦ柟娉曠骇鍒娇鐢ㄨ鏍囨敞鏉ヨ鐩栫被绾у埆鐨勫畾涔夈€?/p>

铏界劧 @Transactional 娉ㄨВ鍙互浣滅敤浜庢帴鍙c€佹帴鍙f柟娉曘€佺被浠ュ強绫绘柟娉曚笂锛屼絾鏄?Spring 寤鸿涓嶈鍦ㄦ帴鍙f垨鑰呮帴鍙f柟娉曚笂浣跨敤璇ユ敞瑙o紝鍥犱负杩欏彧鏈夊湪浣跨敤鍩轰簬鎺ュ彛鐨勪唬鐞嗘椂瀹冩墠浼氱敓鏁堛€傚彟澶栵紝 @Transactional 娉ㄨВ搴旇鍙搴旂敤鍒?public 鏂规硶涓婏紝杩欐槸鐢?Spring AOP 鐨勬湰璐ㄥ喅瀹氱殑銆傚鏋滀綘鍦?protected銆乸rivate 鎴栬€呴粯璁ゅ彲瑙佹€х殑鏂规硶涓婁娇鐢?@Transactional 娉ㄨВ锛岃繖灏嗚蹇界暐锛屼篃涓嶄細鎶涘嚭浠讳綍寮傚父銆?/p>

榛樿鎯呭喌涓嬶紝鍙湁鏉ヨ嚜澶栭儴鐨勬柟娉曡皟鐢ㄦ墠浼氳AOP浠g悊鎹曡幏锛屼篃灏辨槸锛岀被鍐呴儴鏂规硶璋冪敤鏈被鍐呴儴鐨勫叾浠栨柟娉曞苟涓嶄細寮曡捣浜嬪姟琛屼负锛屽嵆浣胯璋冪敤鏂规硶浣跨敤@Transactional娉ㄨВ杩涜淇グ銆?/p>

[java]聽view plain聽copy
  1. @Transactional(readOnly聽=聽true)聽聽
  2. publicclass聽DefaultFooService聽implements聽FooService聽{聽聽
  3. 聽聽聽
  4. 聽聽public聽Foo聽getFoo(String聽fooName)聽{聽聽
  5. 聽聽聽聽//聽do聽something聽聽
  6. 聽聽}聽聽
  7. 聽聽聽
  8. 聽聽//聽these聽settings聽have聽precedence聽for聽this聽method聽聽
  9. 聽聽//鏂规硶涓婃敞瑙e睘鎬т細瑕嗙洊绫绘敞瑙d笂鐨勭浉鍚屽睘鎬?/span>聽聽
  10. 聽聽@Transactional(readOnly聽=聽false,聽propagation聽=聽Propagation.REQUIRES_NEW)聽聽
  11. 聽聽publicvoid聽updateFoo(Foo聽foo)聽{聽聽
  12. 聽聽聽聽//聽do聽something聽聽
  13. 聽聽}聽聽
  14. }聽聽

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有