- 爱易网页
-
数据库教程
- 娣卞叆鐞嗚ВJDBC鐨勮秴鏃惰缃
日期:2014-05-16 浏览次数:25312 次
娣卞叆鐞嗚ВJDBC鐨勮秴鏃惰缃?
鎭板綋鐨凧DBC瓒呮椂璁剧疆鑳藉鏈夋晥鍦板噺灏戞湇鍔″け鏁堢殑鏃堕棿銆傛湰鏂囧皢瀵规暟鎹簱鐨勫悇绉嶈秴鏃惰缃強鍏惰缃柟娉曞仛浠嬬粛銆?
鐪熷疄妗堜緥锛氬簲鐢ㄦ湇鍔″櫒鍦ㄩ伃鍒癉Dos鏀诲嚮鍚庢棤娉曞搷搴?
鍦ㄩ伃鍒癉Dos鏀诲嚮鍚庯紝鏁翠釜鏈嶅姟閮藉灝鎺変簡銆傜敱浜庣鍥涘眰浜ゆ崲鏈轰笉鍫噸璐燂紝缃戠粶鍙樺緱鏃犳硶杩炴帴锛屼粠鑰屽鑷翠笟鍔$郴缁熶篃鏃犳硶姝e父杩愯浆銆傚畨鍏ㄧ粍寰堝揩灞忚斀浜嗘墍鏈夌殑DDos鏀诲嚮锛屽苟鎭㈠浜嗙綉缁滐紝浣嗕笟鍔$郴缁熷嵈杩樻槸鏃犳硶宸ヤ綔銆?閫氳繃鍒嗘瀽绯荤粺鐨則hread dump鍙戠幇锛屼笟鍔$郴缁熷仠鍦ㄤ簡JDBC API鐨勮皟鐢ㄤ笂銆?0鍒嗛挓鍚庯紝绯荤粺浠嶅浜嶹AITING鐘舵€侊紝鏃犳硶鍝嶅簲銆?0鍒嗛挓鍚庯紝绯荤粺鎶涘嚭寮傚父锛屾湇鍔℃仮澶嶆甯搞€?
涓轰粈涔堟垜浠槑鏄庡皢query timeout璁剧疆鎴愪簡3绉掞紝绯荤粺鍗存寔缁簡30鍒嗛挓鐨刉AITING鐘舵€侊紵涓轰粈涔?0鍒嗛挓鍚庣郴缁熷張鎭㈠姝e父浜嗭紵 褰撲綘瀵圭悊瑙d簡JDBC鐨勮秴鏃惰缃悗锛屽氨鑳芥壘鍒伴棶棰樼殑绛旀銆?
涓轰粈涔堟垜浠浜嗚ВJDBC锛?
褰撻亣鍒版€ц兘闂鎴栫郴缁熷嚭閿欐椂锛屼笟鍔$郴缁熷拰鏁版嵁搴撻€氬父鏄垜浠渶鍏冲績鐨勪袱涓儴鍒嗐€傚湪鍏徃閲岋紝杩欎袱涓儴鍒嗘槸浜ょ敱涓や釜涓嶅悓鐨勯儴闂ㄦ潵璐熻矗鐨勶紝鍥犳鍚勪釜閮ㄩ棬閮戒細闆嗕腑绮惧姏鍦板湪鑷韩棰嗗煙鍐呭鎵鹃棶棰橈紝杩欐牱鐨勮瘽锛屽湪涓氬姟绯荤粺鍜屾暟鎹簱涔嬮棿鐨勯儴鍒嗗氨浼氭垚涓轰竴涓洸鍖恒€傚浜嶫ava搴旂敤鑰岃█锛岃繖涓洸鍖哄氨鏄疍BCP鏁版嵁搴撹繛鎺ユ睜鍜孞DBC锛屾湰鏂囧皢闆嗕腑浠嬬粛JDBC銆?
浠€涔堟槸JDBC锛?
JDBC鏄疛ava搴旂敤涓敤鏉ヨ繛鎺ュ叧绯诲瀷鏁版嵁搴撶殑鏍囧噯API銆係un鍏徃涓€鍏卞畾涔変簡4绉嶇被鍨嬬殑JDBC锛屾垜浠富瑕佷娇鐢ㄧ殑鏄4绉嶏紝璇ョ被鍨嬬殑Driver瀹屽叏鐢盝ava浠g爜瀹炵幇锛岄€氳繃浣跨敤socket涓庢暟鎹簱杩涜閫氫俊銆?
鍥? JDBC Type 4.
绗?绉嶇被鍨嬬殑JDBC閫氳繃socket瀵瑰瓧鑺傛祦杩涜澶勭悊锛屽洜姝や篃浼氭湁涓€浜涘熀鏈綉缁滄搷浣滐紝绫讳技浜嶩ttpClient杩欑鐢ㄤ簬缃戠粶鎿嶄綔鐨勪唬鐮佸簱銆傚綋鍦ㄧ綉缁滄搷浣滀腑閬囧埌闂鐨勬椂鍊欙紝灏嗕細娑堣€楀ぇ閲忕殑cpu璧勬簮锛屽苟涓斿け鍘诲搷搴旇秴鏃躲€傚鏋滀綘涔嬪墠鐢ㄨ繃HttpClient锛岄偅涔堜綘涓€瀹氶亣鍒拌繃鏈缃畉imeout閫犳垚鐨勯敊璇€傚悓鏍凤紝绗?绉嶇被鍨嬬殑JDBC锛岃嫢娌℃湁鍚堢悊鍦拌缃畇ocket timeout锛屼篃浼氭湁鐩稿悓鐨勯敊璇€斺€旇繛鎺ヨ闃诲銆?
鎺ヤ笅鏉ワ紝灏辫鎴戜滑鏉ュ涔犱竴涓嬪浣曟纭湴璁剧疆socket timeout锛屼互鍙婇渶瑕佽€冭檻鐨勯棶棰樸€?
搴旂敤涓庢暟鎹簱闂寸殑timeout灞傜骇
鍥? Timeout Class.
涓婂浘灞曠ず浜嗙畝鍖栧悗搴旂敤涓庢暟鎹簱闂寸殑timeout灞傜骇銆傦紙璇戣€呮敞锛歐AS/BLOC鏄綔鑰呭叕鍙哥殑鍏蜂綋搴旂敤鍚嶇О锛屾棤闇€娣辩┒锛?
楂樼骇鍒殑timeout渚濊禆浜庝綆绾у埆鐨則imeout锛屽彧鏈夊綋浣庣骇鍒殑timeout鏃犺鏃讹紝楂樼骇鍒殑timeout鎵嶈兘纭繚姝e父銆備緥濡傦紝褰搒ocket timeout鍑虹幇闂鏃讹紝楂樼骇鍒殑statement timeout鍜宼ransaction timeout閮藉皢澶辨晥銆?
鎴戜滑鏀跺埌鐨勫緢澶氳瘎璁轰腑鎻愬埌锛?
寮曠敤
鍗充娇璁剧疆浜唖tatement timeout锛屽綋缃戠粶鍑洪敊鏃讹紝搴旂敤涔熸棤娉曚粠閿欒涓仮澶嶃€?
statement timeout鏃犳硶澶勭悊缃戠粶杩炴帴澶辫触鏃剁殑瓒呮椂锛屽畠鑳藉仛鐨勪粎浠呮槸闄愬埗statement鐨勬搷浣滄椂闂淬€傜綉缁滆繛鎺ュけ璐ユ椂鐨則imeout蹇呴』浜ょ敱JDBC鏉ュ鐞嗐€?
JDBC鐨剆ocket timeout浼氬彈鍒版搷浣滅郴缁焥ocket timeout璁剧疆鐨勫奖鍝嶏紝杩欏氨瑙i噴浜嗕负浠€涔堝湪涔嬪墠鐨勬渚嬩腑锛孞DBC杩炴帴浼氬湪缃戠粶鍑洪敊鍚庨樆濉?0鍒嗛挓锛岀劧鍚庡張濂囪抗鑸仮澶嶏紝鍗充娇鎴戜滑骞舵病鏈夊JDBC鐨剆ocket timeout杩涜璁剧疆銆?
DBCP杩炴帴姹犱綅浜庡浘2鐨勫乏渚э紝浣犱細鍙戠幇timeout灞傜骇涓嶥BCP鏄浉浜掔嫭绔嬬殑銆侱BCP璐熻矗鐨勬槸鏁版嵁搴撹繛鎺ョ殑鍒涘缓鍜岀鐞嗭紝骞朵笉骞叉秹timeout鐨勫鐞嗐€傚綋杩炴帴鍦―BCP涓垱寤猴紝鎴栨槸DBCP鍙戦€佹牎楠宷uery妫€鏌ヨ繛鎺ユ湁鏁堟€х殑鏃跺€欙紝socket timeout灏嗕細褰卞搷杩欎簺杩囩▼锛屼絾骞朵笉鐩存帴瀵瑰簲鐢ㄩ€犳垚褰卞搷銆?
褰撳湪搴旂敤涓皟鐢―BCP鐨刧etConnection()鏂规硶鏃讹紝浣犲彲浠ヨ缃幏鍙栨暟鎹簱杩炴帴鐨勮秴鏃舵椂闂达紝浣嗘槸杩欏拰JDBC鐨則imeout姣笉鐩稿叧銆?
鍥? Timeout for Each Levels.
浠€涔堟槸Transaction Timeout锛?
transaction timeout涓€鑸瓨鍦ㄤ簬妗嗘灦锛圫pring, EJB锛夋垨搴旂敤绾с€倀ransaction timeout鎴栬鏄釜鐩稿闄岀敓鐨勬蹇碉紝绠€鍗曞湴璇达紝transaction timeout灏辨槸鈥渟tatement Timeout * N锛堥渶瑕佹墽琛岀殑statement鏁伴噺锛?+ @锛堝瀮鍦惧洖鏀剁瓑鍏朵粬鏃堕棿锛夆€濄€倀ransaction timeout鐢ㄦ潵闄愬埗鎵цstatement鐨勬€绘椂闀裤€?
渚嬪锛屽亣璁炬墽琛屼竴涓猻tatement闇€瑕?.1绉掞紝閭d箞鎵ц灏戦噺statement涓嶄細鏈変粈涔堥棶棰橈紝浣嗚嫢鏄鎵ц100,000涓猻tatement鍒欓渶瑕?0,000绉掞紙绾?涓皬鏃讹級銆傝繖鏃讹紝transaction timeout灏辨淳涓婄敤鍦轰簡銆侲JB CMT (Container Managed Transaction)灏辨槸涓€绉嶅吀鍨嬬殑瀹炵幇锛屽畠鎻愪緵浜嗗绉嶆柟娉曚緵寮€鍙戣€呴€夋嫨銆備絾鎴戜滑骞朵笉浣跨敤EJB锛孲pring鐨則ransaction timeout璁剧疆浼氭洿甯哥敤涓€浜涖€傚湪Spring涓紝浣犲彲浠ヤ娇鐢ㄤ笅闈㈠睍绀虹殑XML鎴栨槸鍦ㄦ簮鐮佷腑浣跨敤@Transactional娉ㄨВ鏉ヨ繘琛岃缃€?
Xml浠g爜
<tx:attributes>
<tx:method name=鈥溾€︹€?timeout=鈥?鈥?>
</tx:attributes>
Spring鎻愪緵鐨則ransaction timeout閰嶇疆闈炲父绠€鍗曪紝瀹冧細璁板綍姣忎釜浜嬪姟鐨勫紑濮嬫椂闂村拰娑堣€楁椂闂达紝褰撶壒瀹氱殑浜嬩欢鍙戠敓鏃跺氨浼氬娑堣€楁椂闂村仛鏍¢獙锛屽綋瓒呭嚭timeout鍊兼椂灏嗘姏鍑哄紓甯搞€?
Spring涓紝鏁版嵁搴撹繛鎺ヨ淇濆瓨鍦═hreadLocal閲岋紝杩欒绉颁负浜嬪姟鍚屾锛圱ransaction Synchronization锛夛紝涓庢鍚屾椂锛屼簨鍔$殑寮€濮嬫椂闂村拰娑堣€楁椂闂翠篃琚繚瀛樹笅鏉ャ€傚綋浣跨敤杩欑浠g悊杩炴帴鍒涘缓statement鏃讹紝灏变細鏍¢獙浜嬪姟鐨勬秷鑰楁椂闂淬€侲JB CMT鐨勫疄鐜版柟寮忎笌涔嬬被浼硷紝鍏剁粨鏋勬湰韬篃鍗佸垎绠€鍗曘€?
褰撲綘閫夌敤鐨勫鍣ㄦ垨妗嗘灦骞朵笉鏀寔transaction timeout杩欎竴鐗规€э紝浣犲彲浠ヨ€冭檻鑷繁鏉ュ疄鐜般€倀ransaction timeout骞舵病鏈夋爣鍑嗙殑API銆侺ucy妗嗘灦鐨?.5鍜?.6鐗堟湰閮戒笉鏀寔transaction timeout锛屼絾鏄綘鍙互閫氳繃浣跨敤Spring鐨凾ransaction Manager鏉ヨ揪鍒颁笌涔嬪悓鏍风殑鏁堟灉銆?
鍋囪鏌愪釜浜嬪姟涓寘鍚?涓猻tatement锛屾瘡涓猻tatement鐨勬墽琛屾椂闂存槸200ms锛屽叾浠栦笟鍔¢€昏緫鐨勬墽琛屾椂闂存槸100ms锛岄偅涔坱ransaction timeout鑷冲皯搴旇璁剧疆涓?,100ms锛?00 * 5 + 100锛夈€?