`
mywebcode
  • 浏览: 998399 次
文章分类
社区版块
存档分类
最新评论

hibernate,spring管理事务中(transaction,JDBC connection,Hibernate Session的使用)

 
阅读更多

首先hibernate的Connection release mode有以下几种:
1 after_statement 2 after_transaction 3 on_close 其中after_statement 用在jta中 ,on_close 是3.1之前遗留的(也许是为spring留的-_-),也就是3.1之前默认是on_close ,但3.1之后默认如果单独使用hibernate是after_transaction,如果有第三方事务管理,就用第三方提供的默认值,spring就是默认使用了on_close。
在spring管理事务中我们看看系统启动后默认使用的配置:
1,ransaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory使用spring事务策略
2,hibernate内部 Automatic session close at end of transaction: disabled 因为已经交给spring了
3 Connection release mode: auto 默认,也就是没有配置hibernate.connection.release_mode的时候,但是这里有地方需要注意:也就是前面提到的使用第三方策略时的问题:看一下代码:
StringreleaseModeName=PropertiesHelper.getString(Environment.RELEASE_CONNECTIONS,properties,"auto");
log.info("Connectionreleasemode:"+releaseModeName);
ConnectionReleaseModereleaseMode;
if("auto".equals(releaseModeName)){
releaseMode=transactionFactory.getDefaultReleaseMode();}

else{
releaseMode=ConnectionReleaseMode.parse(releaseModeName);
if(releaseMode==ConnectionReleaseMode.AFTER_STATEMENT&&!connections.supportsAggressiveRelease()){
log.warn("Overridingreleasemodeasconnectionproviderdoesnotsupport'after_statement'");
releaseMode=ConnectionReleaseMode.AFTER_TRANSACTION;
}

}
其中红色部分就是调用了spring提供的默认值,而spring的默认值:在jta和cmt中都默认使用的是after_statement
/**
*Setsconnectionreleasemode"on_close"asdefault.
*<p>ThiswasthecaseforHibernate3.0;Hibernate3.1changed
*itto"auto"(i.e."after_statement"or"after_transaction").
*However,forSpring'sresourcemanagement(inparticularfor
*HibernateTransactionManager),"on_close"isthebetterdefault.
*/

publicConnectionReleaseModegetDefaultReleaseMode(){
returnConnectionReleaseMode.ON_CLOSE;
}


而spring为什么要使用on_close ,而不是用after_transaction ,我们想想opensessioninview的原理也许能明白,session在view成还要使用,所以不能再transaction使用完后关闭JDBC connection,必须要在session之后,所以要使用on_close(也就是在on session(flush.auto,或者flush.Eagerly) 关闭)。这种情况hibernate内部还会在spring关闭JDBC connection后提示(费解,因为after transaction之后session没有关闭,但是Connection release mode配置的是on_close,session的关闭和Connection 的关闭都由spring来管理,hibernate就不知道了),所以hibernate有好的提示如下(其实session,已经关闭。当然随着session的关闭jdbc链接释放回连接池):
transaction completed on session with on_close connection release mode; be sure to cl
ose the session to release JDBC resources!,

。当然我们也可以使用after_transaction ,这种情况对使用编程式事务非常适用。
分享到:
评论

相关推荐

    ssh(structs,spring,hibernate)框架中的上传下载

    3在Spring中使用org.springframework.jdbc.support.lob.OracleLobHandler处理Oracle数据库的Blob类型字段。  通过这样的设置和配置,我们就可以象持久化表的一般字段类型一样处理Blob字段了。  以上是Spring+...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    低清版 大型门户网站是这样炼成的.pdf

    5.1.1 使用session的begintransaction()方法 272 5.1.2 使用session的close()方法 273 5.1.3 使用session的connection()方法 273 5.1.4 使用session的delete()方法 274 5.1.5 使用session的get()方法 274 5.1.6...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    spring-boot-reference.pdf

    41. Spring Session 42. Monitoring and Management over JMX 43. Testing 43.1. Test Scope Dependencies 43.2. Testing Spring Applications 43.3. Testing Spring Boot Applications 43.3.1. Detecting Web ...

    客户关系管理系统框架搭建(二)

    &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/itcast0807crm &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt; &lt;property name="...

    ibatis 开发指南(pdf)

    35 基于JDBC 的事务管理机制................................................................ 35 基于JTA 的事务管理机制................................................................... 36 ...

Global site tag (gtag.js) - Google Analytics