友情链接1:21点 足球投注 投注网 明升体育 hg0088 葡京赌场 网上赌球 诈金花 澳门金沙网址 澳门银河网址 澳门银河 澳门银河官网 银河官网 威尼斯人 澳门威尼斯人 澳门威尼斯人官网 威尼斯人官网 金沙 澳门金沙 澳门金沙官网 金沙官网 现金网 赌球网 全讯网 明升国际 足球开户 ******官网 ******网址 现金赌博 澳门现金网 ******官方网站

OpenSessionInViewFilter | Mingo's Blog
鏃ュ織鏍囩锛歄penSessionInViewFilter

OpenSessionInViewFilter 璇﹁В

鏃堕棿锛2012骞05鏈06鏃浣滆咃細Mingo琚洿瑙備簡锛7,864 娆璇勮娆℃暟锛3

openSessionInViewFilter鏄疭pring鎻愪緵鐨勪竴涓拡瀵笻ibernate鐨勪竴涓敮鎸佺被锛屽叾涓昏鎰忔濇槸鍦ㄥ彂璧蜂竴涓〉闈㈣姹傛椂鎵撳紑Hibernate鐨凷ession锛屼竴鐩翠繚鎸佽繖涓猄ession锛岀洿鍒拌繖涓姹傜粨鏉燂紝鍏蜂綋鏄氳繃涓涓狥ilter鏉ュ疄鐜扮殑銆

鐢变簬Hibernate寮曞叆浜哃azy Load鐗规э紝浣垮緱鑴辩Hibernate鐨凷ession鍛ㄦ湡鐨勫璞″鏋滃啀鎯抽氳繃getter鏂规硶鍙栧埌鍏跺叧鑱斿璞$殑鍊硷紝Hibernate浼氭姏鍑轰竴涓狶azyLoad鐨凟xception銆傛墍浠ヤ负浜嗚В鍐宠繖涓棶棰橈紝Spring寮曞叆浜嗚繖涓狥ilter锛屼娇寰桯ibernate鐨凷ession鐨勭敓鍛藉懆鏈熷彉闀裤

棣栧厛鍒嗘瀽涓涓嬪畠鐨勬簮鐮侊紝鍙互鍙戠幇锛屽畠鎵瀹炵幇鐨勫姛鑳藉叾瀹炴瘮杈冪畝鍗曪細

SessionFactory sessionFactory = lookupSessionFactory(request);
Session session = null;
boolean participate = false;   

if (isSingleSession()) {
    // single session mode
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
    // Do not modify the Session: just set the participate flag.
    participate = true;
       }    else {
    logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
    session = getSession(sessionFactory);
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }
} else {
    // deferred close mode
    if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
// Do not modify deferred close: just set the participate flag.
    participate = true;
    } else {
    SessionFactoryUtils.initDeferredClose(sessionFactory);
    }
}   

try {
    filterChain.doFilter(request, response);
} finally {
    if (!participate) {
            if (isSingleSession()) {
                // single session mode
        TransactionSynchronizationManager.unbindResource(sessionFactory);
        logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");
        closeSession(session, sessionFactory);
    }else {
        // deferred close mode
        SessionFactoryUtils.processDeferredClose(sessionFactory);
    }
}
}  

SessionFactory sessionFactory = lookupSessionFactory(request);
Session session = null;
boolean participate = false;

if (isSingleSession()) {
	// single session mode
	if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
	// Do not modify the Session: just set the participate flag.
	participate = true;
       }	else {
	logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
	session = getSession(sessionFactory);
	TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
	}
} else {
	// deferred close mode
	if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
// Do not modify deferred close: just set the participate flag.
	participate = true;
    } else {
	SessionFactoryUtils.initDeferredClose(sessionFactory);
    }
}

try {
	filterChain.doFilter(request, response);
} finally {
	if (!participate) {
           	if (isSingleSession()) {
	         	// single session mode
		TransactionSynchronizationManager.unbindResource(sessionFactory);
		logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");
		closeSession(session, sessionFactory);
	}else {
		// deferred close mode
		SessionFactoryUtils.processDeferredClose(sessionFactory);
	}
}
}

 

鍦ㄤ笂杩颁唬鐮佷腑锛岄鍏堣幏寰桽essionFactory锛岀劧鍚庨氳繃SessionFactory鑾峰緱涓涓猄ession銆傜劧鍚庢墽琛岀湡姝g殑Action浠g爜锛屾渶鍚庢牴鎹儏鍐靛皢Hibernate鐨凷ession杩涜鍏抽棴銆傛暣涓濊矾姣旇緝娓呮櫚銆

娉ㄦ剰锛屽湪杩欓噷鏈変釜2涓猅ips锛
1锛夐氳繃getSession()鑾峰緱鐨勮繖涓猄ession鍋氫簡涓娆
session.setFlushMode(FlushMode.NEVER); 鏈夊叧FlushMode鍙互鍙傝冧竴涓嬭繖绡囨枃绔犮http://www2.matrix.org.cn/resource/article/2006-10-08/Hibernate+FlushMode+NEVER_312bca85-5699-11db-91a0-d98dff0aec60.html
2锛塖pring瀵规嬁鍒扮殑Session鍋氫簡涓娆$粦瀹氬埌褰撳墠绾跨▼鐨勫仛娉曪紝浣垮緱杩欎釜Session鏄嚎绋嬪畨鍏ㄧ殑銆

浠庝笂杩颁唬鐮佸叾瀹炲彲浠ュ緱鍒颁竴浜涘鎴戜滑鐨勫紑鍙戞湁甯姪鐨勭粨璁猴細
1锛夊鏋滀娇鐢ㄤ簡OpenSessionInView妯″紡锛岄偅涔圫pring浼氬府鍔╀綘绠$悊Session鐨勫紑鍜屽叧锛屼粠鑰屼綘鍦ㄤ綘鐨凞AO涓氳繃HibernateDaoSupport鎷垮埌鐨刧etSession()鏂规硶锛岄兘鏄粦瀹氬埌褰撳墠绾跨▼鐨勭嚎绋嬪畨鍏ㄧ殑Session锛屽嵆鎷垮嵆鐢紝鏈鍚庝細鐢盕ilter缁熶竴鍏抽棴銆
2锛夌敱浜庢嬁鍒扮殑Hibernate鐨凷ession琚缃簡session.setFlushMode(FlushMode.NEVER); 鎵浠ワ紝闄ら潪浣犵洿鎺ヨ皟鐢╯ession.flush()锛屽惁鍒橦ibernate session鏃犺浣曟椂涔熶笉浼歠lush浠讳綍鐨勭姸鎬佸彉鍖栧埌鏁版嵁搴撱傚洜姝わ紝鏁版嵁搴撲簨鍔$殑閰嶇疆闈炲父閲嶈銆傦紙鎴戜滑鐭ラ亾锛屽湪璋冪敤org.hibernate.Transaction.commit()鐨勬椂鍊欎細瑙﹀彂session.flush()锛夋垜鏇剧粡瑙佽繃寰堝浜哄湪浣跨敤OpenSessionInView妯″紡鏃讹紝閮藉洜涓烘病鏈夋纭厤缃簨鍔★紝瀵艰嚧浜嗗簳灞備細鎶涘嚭鏈夊叧FlushMode.NEVER鐨勫紓甯搞

OpenSessionInView杩欎釜妯″紡浣跨敤姣旇緝绠鍗曪紝涔熸垚涓轰簡澶у鍦╓eb寮鍙戜腑缁忓父浣跨敤鐨勬柟娉曪紝涓嶈繃瀹冩湁鏃跺欎細甯︽潵涓浜涙剰鎯充笉鍒扮殑闂锛岃繖涔熸槸鍦ㄥ紑鍙戜腑闇瑕佹敞鎰忕殑銆
1. 鍦⊿truts锛婼pring锛婬ibernate鐜涓紝鐢变簬閰嶇疆鐨勯棶棰樺鑷寸殑妯″紡澶辨晥
杩欎釜闂浠ュ墠璁哄潧鏇剧粡璁ㄨ杩囷紝鍙互鍙傝冧竴涓嬩笅闈㈣繖涓笘瀛愶細
http://www.iteye.com/topic/15057

2. OpenSessionInView鐨勬晥鐜囬棶棰
杩欎釜闂涔熸湁浜哄湪璁哄潧鎻愬嚭杩囷紝Robbin鏇剧粡鍋氳繃鍏蜂綋鐨勬祴璇曪紝鍙互鍏蜂綋鍙傝冧竴涓嬩笅闈㈣繖涓笘瀛愶細
http://www.iteye.com/topic/17501

3. 鐢变簬浣跨敤浜哋penSessionInView妯″紡鍚庨犳垚浜嗗唴瀛樺拰鏁版嵁搴撹繛鎺ラ棶棰
杩欎釜闂鏄垜鍦ㄧ敓浜х幆澧冧腑纰板埌鐨勪竴涓棶棰樸傜敱浜庝娇鐢ㄤ簡OpenSessionInView妯″紡锛孲ession鐨勭敓鍛藉懆鏈熷彉寰楅潪甯搁暱銆傝櫧鐒惰В鍐充簡Lazy Load鐨勯棶棰橈紝浣嗘槸甯︽潵鐨勯棶棰樺氨鏄疕ibernate鐨勪竴绾х紦瀛橈紝涔熷氨鏄疭ession绾у埆鐨勭紦瀛樼殑鐢熷懡鍛ㄦ湡浼氬彉寰楅潪甯搁暱锛岄偅涔堝鏋滀綘鍦ㄤ綘鐨凷ervice灞傚仛澶ф壒閲忕殑鏁版嵁鎿嶄綔鏃讹紝鍏跺疄杩欎簺鏁版嵁浼氬湪缂撳瓨涓繚鐣欎竴浠斤紝杩欐槸闈炲父鑰楄垂鍐呭瓨鐨勩傝繕鏈変竴涓暟鎹簱杩炴帴鐨勯棶棰橈紝瀛樺湪鐨勫師鍥犲湪浜庣敱浜庢暟鎹簱鐨凜onnection鏄拰Session缁戝湪涓璧风殑锛屾墍浠ワ紝Connection涔熶細寰椾笉鍒板強鏃剁殑閲婃斁銆傚洜鑰屽綋绯荤粺鍑虹幇涓氬姟闈炲父绻佸繖锛岃岃绠楅噺鍙堥潪甯稿ぇ鐨勬椂鍊欙紝寰寰鏁版嵁杩炴帴姹犵殑杩炴帴鏁颁細涓嶅銆傝繖涓棶棰樻垜鑷充粖闈炲父澶寸棝锛屽洜涓烘湁寰堝瀹㈡埛瀵规暟鎹繛鎺ユ睜鐨勬暟閲忎細鏈夐檺鍒讹紝涓嶄細缁欎綘鏃犻檺鍒剁殑澧炲姞涓嬪幓銆

4. 浣跨敤浜哋penSessionInView妯″紡浠ュ悗鍙栨暟鎹殑浜嬪姟闂
鍦ㄤ娇鐢ㄤ簡OpenSessionInView浠ュ悗锛屽叾瀹炰簨鍔$殑鐢熷懡鍛ㄦ湡姣擲ession鐨勭敓鍛藉懆鏈熸潵寰楃煭锛屽氨浠ヤ负鐫锛屽叾瀹炴湁鐩稿綋涓閮ㄥ垎鐨勬煡璇㈡槸涓嶈绾冲叆鍒颁簨鍔$殑鑼冨洿鍐呯殑锛屾鏃舵槸鍚︿細璇诲埌鑴忔暟鎹紵杩欎釜闂鎴戣嚦浠婁笉鏁㈢‘璁わ紝鏈夌粡楠岀殑鏈嬪弸璇锋寚鏁欎竴涓嬨

鏈鍚庢彁涓涓婳penSessionInView妯″紡鐨勪竴浜涙浛浠f柟妗堬紝鍙互浣跨敤OpenSessionInViewInterceptor鏉ヤ唬鏇胯繖涓狥ilter锛屾鏃跺彲浠ヤ娇鐢⊿pring鐨凙OP閰嶇疆锛屽皢杩欎釜Interceptor閰嶇疆鍒颁綘鎵闇瑕佺殑灞傛涓婂幓銆傚彟澶栧氨鏄彧鑳戒娇鐢ㄦ渶鍙よ佺殑Hibernate.initialize()鏂规硶杩涜鍒濆鍖栦簡銆