 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
3 o: P2 r0 S2 ^0 Y2 k- A基本参数:# o, o0 }7 b' o# i# ]7 X
jta | thread | managed | custom.Class+ t7 v# d8 H% I( `& A
主要是配合
+ M1 C/ n+ b4 G7 @6 i0 Y9 GSessionFactory.getCurrentSession()) D4 U' ^1 |7 t5 r/ t( c" z* q
来用的.
' A9 f& N# P W4 G$ c5 ?2 N. T$ xThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
0 J# _6 |. a/ `) fWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
4 B D0 }* o4 h% u- r Dcontext.BeginRequest += OpenSession;
4 [3 @# s4 m, U X" v: }2 w.Bind(HttpContext.Current, SessionFactory.OpenSession())* P* k I5 N- k$ h- l7 Y- Y) T( c
$ |3 l* `/ G; H8 H
//do transaction O/ K0 n7 W3 L+ Q+ l, U5 |+ r
getCurrentSession().Submit();( Q2 h( ]2 c7 Z/ N& I
getCurrentSession().RollBack();
; G% n# [6 u, `+ \1 k7 f- P...+ R5 ^' q4 n0 q% t! l( M3 m
Unbind(HttpContext.Current, SessionFactory)# U3 G6 m7 r1 ~
8 @8 V) W4 w* N5 A1 S; r$ L/ a
context.EndRequest += CloseSession;8 D; w) a2 T9 i
# n9 y+ i1 n2 Q" S# d 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|