hibernate,spring管理事務(wù)中(transaction,JDBC connection,Hibernate Session的使用研究)(一)
Posted on 2011-11-18 11:25 瘋狂 閱讀(18929) 評(píng)論(0) 編輯 收藏 所屬分類: database 、spring 、hibernate 、方法論 、架構(gòu) 、讀代碼如果單獨(dú)使用hibernate可參考上一篇文章http://www.aygfsteel.com/freeman1984/archive/2011/08/04/355808.html
首先hibernate的Connection release mode有以下幾種:
1 after_statement 2 after_transaction 3 on_close 其中after_statement 用在jta中 ,on_close 是3.1之前遺留的(也許是為spring留的-_-),也就是3.1之前默認(rèn)是on_close ,但3.1之后默認(rèn)如果單獨(dú)使用hibernate是after_transaction,如果有第三方事務(wù)管理,就用第三方提供的默認(rèn)值,spring就是默認(rèn)使用了on_close。
在spring管理事務(wù)中我們看看系統(tǒng)啟動(dòng)后默認(rèn)使用的配置:
1,ransaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory使用spring事務(wù)策略
2,hibernate內(nèi)部 Automatic session close at end of transaction: disabled 因?yàn)橐呀?jīng)交給spring了
3 Connection release mode: auto 默認(rèn),也就是沒(méi)有配置hibernate.connection.release_mode的時(shí)候,但是這里有地方需要注意:也就是前面提到的使用第三方策略時(shí)的問(wèn)題:看一下代碼:
String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
log.info( "Connection release mode: " + releaseModeName );
ConnectionReleaseMode releaseMode;
if ( "auto".equals(releaseModeName) ) {
releaseMode = transactionFactory.getDefaultReleaseMode(); }

else {
releaseMode = ConnectionReleaseMode.parse( releaseModeName );
if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
}
}其中紅色部分就是調(diào)用了spring提供的默認(rèn)值,而spring的默認(rèn)值:在jta和cmt中都默認(rèn)使用的是after_statement
/**
* Sets connection release mode "on_close" as default.
* <p>This was the case for Hibernate 3.0; Hibernate 3.1 changed
* it to "auto" (i.e. "after_statement" or "after_transaction").
* However, for Spring's resource management (in particular for
* HibernateTransactionManager), "on_close" is the better default.
*/
public ConnectionReleaseMode getDefaultReleaseMode() {
return ConnectionReleaseMode.ON_CLOSE;
}

而spring為什么要使用on_close ,而不是用after_transaction ,我們想想opensessioninview的原理也許能明白,session在view成還要使用,所以不能再transaction使用完后關(guān)閉JDBC connection,必須要在session之后,所以要使用on_close(也就是在on session(flush.auto,或者flush.Eagerly) 關(guān)閉)。這種情況hibernate內(nèi)部還會(huì)在spring關(guān)閉JDBC connection后提示(費(fèi)解,因?yàn)閍fter transaction之后session沒(méi)有關(guān)閉,但是 Connection release mode配置的是on_close,session的關(guān)閉和 Connection 的關(guān)閉都由spring來(lái)管理,hibernate就不知道了),所以hibernate有好的提示如下(其實(shí)session,已經(jīng)關(guān)閉。當(dāng)然隨著session的關(guān)閉jdbc鏈接釋放回連接池):
transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!,
。當(dāng)然我們也可以使用after_transaction ,這種情況對(duì)使用編程式事務(wù)非常適用。
首先hibernate的Connection release mode有以下幾種:
1 after_statement 2 after_transaction 3 on_close 其中after_statement 用在jta中 ,on_close 是3.1之前遺留的(也許是為spring留的-_-),也就是3.1之前默認(rèn)是on_close ,但3.1之后默認(rèn)如果單獨(dú)使用hibernate是after_transaction,如果有第三方事務(wù)管理,就用第三方提供的默認(rèn)值,spring就是默認(rèn)使用了on_close。
在spring管理事務(wù)中我們看看系統(tǒng)啟動(dòng)后默認(rèn)使用的配置:
1,ransaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory使用spring事務(wù)策略
2,hibernate內(nèi)部 Automatic session close at end of transaction: disabled 因?yàn)橐呀?jīng)交給spring了
3 Connection release mode: auto 默認(rèn),也就是沒(méi)有配置hibernate.connection.release_mode的時(shí)候,但是這里有地方需要注意:也就是前面提到的使用第三方策略時(shí)的問(wèn)題:看一下代碼:
























而spring為什么要使用on_close ,而不是用after_transaction ,我們想想opensessioninview的原理也許能明白,session在view成還要使用,所以不能再transaction使用完后關(guān)閉JDBC connection,必須要在session之后,所以要使用on_close(也就是在on session(flush.auto,或者flush.Eagerly) 關(guān)閉)。這種情況hibernate內(nèi)部還會(huì)在spring關(guān)閉JDBC connection后提示(費(fèi)解,因?yàn)閍fter transaction之后session沒(méi)有關(guān)閉,但是 Connection release mode配置的是on_close,session的關(guān)閉和 Connection 的關(guān)閉都由spring來(lái)管理,hibernate就不知道了),所以hibernate有好的提示如下(其實(shí)session,已經(jīng)關(guān)閉。當(dāng)然隨著session的關(guān)閉jdbc鏈接釋放回連接池):
transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!,
。當(dāng)然我們也可以使用after_transaction ,這種情況對(duì)使用編程式事務(wù)非常適用。