 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; d# ?$ M2 n2 {0 P( E基本参数:. ^) _2 Z( a2 J
jta | thread | managed | custom.Class! T6 d) W, ^! _8 D$ @
主要是配合0 q: e0 y! B2 m- `
SessionFactory.getCurrentSession()% e5 N5 A* h. F# b0 L: Z: x
来用的.
9 z. E1 Z! W Y: N- ~* e3 OThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
- z8 r, T5 Z2 g) O: l1 F4 D5 aWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:3 I4 |! Q6 }! u' t* r
context.BeginRequest += OpenSession;2 a+ T" m) C, B
.Bind(HttpContext.Current, SessionFactory.OpenSession())0 K+ t: T! S0 K; L/ f* m9 Y' {
1 K. L; ^7 K2 q4 D' n0 T//do transaction
, P* z) ~. M5 P8 F) ngetCurrentSession().Submit();
$ i1 s) t4 G9 C5 Q7 R7 R! g# @getCurrentSession().RollBack();
~, X B/ M2 J7 ~/ N& l...
, K. b' r% b9 X8 RUnbind(HttpContext.Current, SessionFactory)
% b8 a* c+ k- L% L' Y: a' B6 W A' X+ h( L
context.EndRequest += CloseSession;, H$ c2 W1 Z$ B3 r
* R& h3 x" z; W+ ?, H0 l
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|