 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
7 |! p) ?0 t, Q( p' n0 F基本参数:* y* i: Z O( Y/ e& a- e+ N
jta | thread | managed | custom.Class
( {; p5 k+ |6 s7 y5 [6 T/ W主要是配合9 h% \8 m- t3 D _" d
SessionFactory.getCurrentSession()# ^$ p# c1 Q% G3 R6 r
来用的.
6 Q8 h* A: V" J$ ]7 v$ \* c6 ]Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,3 t6 G0 l1 t. U+ c7 ^6 ], E0 L
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
+ }8 ]3 ` u' ]" `1 Qcontext.BeginRequest += OpenSession;
- R3 \" l8 F! Q3 S' W.Bind(HttpContext.Current, SessionFactory.OpenSession())2 n. @& V: f7 V
! \* a z# p- a6 X4 H! ~% ]0 D, J, E//do transaction% `! Q) G# t+ O8 J
getCurrentSession().Submit();% n4 Y6 A0 `- |' h% m- R/ e6 {, q( [
getCurrentSession().RollBack();. }4 m# @/ m! J1 s3 B
...* `( _& r$ ^0 d4 ?
Unbind(HttpContext.Current, SessionFactory)
; U& i9 Q) Z* z" k( U8 H. i$ f
. ~/ \, |4 \6 L7 S- c' Mcontext.EndRequest += CloseSession;
8 V7 o1 p% p( B O) p* b# x% E9 H# P/ T' a9 B
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|