关于ThreadLocalSession和Transaction之间的关系

org.hibernate.context.ThreadLocalSessionContext is: A CurrentSessionContext impl which scopes the notion of current session by the current thread of execution. Unlike the JTA counterpart, threads do not give us a nice hook to perform any type of cleanup making it questionable for this impl to actually generate Session instances. In the interest of usability, it was decided to have this default impl actually generate a session upon first request and then clean it up after the org.hibernate.Transaction associated with that session is committed/rolled-back. In order for ensuring that happens, the sessions generated here are unusable until after Session.beginTransaction() has been called. If close() is called on a session managed by this class, it will be automatically unbound.

Additionally, the static bind and unbind methods are provided to allow application code to explicitly control opening and closing of these sessions. This, with some from of interception, is the preferred approach. It also allows easy framework integration and one possible approach for implementing long-sessions.

如果只从 ThreadLocalSessionContext的名称上看,它所做的就是把一个session绑定到当前线程上,让当前线程作为session的上下文。这样,在service里不同的dao通过sessionFactory.getCurrentSession()得到的将是当前线程上的同一个session,这是非常必要的做法,通过使用同一个session确保了持久化对象的一致性。但是从 ThreadLocalSessionContext的代码来看,它并只是做了这一件事,它还做了另外一件非常“醒目”的事情:即给session包裹了一层代理org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper,这个代理将拦截session的操作,对session的使用做出了如下的限制:

1.在没有显式地开始一个事务之前,不能使用session的任何数据访问方法。

2.一旦事务提交,session将自动close。

这种做法的意图非常明显,它将transaction的周期与session的同期也绑定在了一起, 实现了所谓的“session-per-request”编程模型。session-per-request就是session-per-transaction,这种编程模型把用户的一次请求做为一次独立的事物来处理,使用一个对应的session.这样,request(也就是thread),transaction,session三者的生命周期就变成一致的了。对绝大多数的应用, session-per-request几乎总是合理和正确的。在没有第三方管理事务或session(如jta和spring)的情况下,使用hibernate内置的threadlocal上下文来管理session是一个默认的选择。需要特别说明的是:如果在这种方式下,提交了一个事务之后,再使用getCurrentSession()来获取session时,将会得到一个新的session,旧的session已经从threadlocal中移除。但如果是我们是在实际应用中遇到这种情况,我们需要审视自己的设计是否真得需要这样做,还是我们的transaction提交的太早了?

 

关于Spring对Session的管理需要另作分析,

一方面,需要了解SpringSessionContext

另一方面,很多处理会和 HibernateTemplat有关系。从Spring的用户手册上看以知道:

HibernateTemplate 会确保当前Hibernate的 Session 实例的正确打开和关闭,并直接参与到事务管理中去。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Laurence 

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值