 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
0 F# W- v7 G0 Z" m# {. s* y, T7 g基本参数:1 z4 }$ Z0 I* @; q9 S; n/ T
jta | thread | managed | custom.Class
" n) w9 d9 Q+ s- ]. N5 R% _主要是配合; c4 Y$ ~% ?3 u
SessionFactory.getCurrentSession()
" B! h+ M7 [) U来用的.4 ^# J% c3 n' y+ L; t! ^! o( }
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
. S( `3 T% w& H) L+ e% VWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:6 P; |, D, p V5 M' C# f7 ?
context.BeginRequest += OpenSession;
, W3 W- T( P& U# ]+ I.Bind(HttpContext.Current, SessionFactory.OpenSession())
5 m1 ]/ H/ @; G! N: E8 [
6 ?6 V$ e2 q! E& S3 `//do transaction
, u- v i* O, Y8 P- vgetCurrentSession().Submit();4 d0 q g- {2 w6 R
getCurrentSession().RollBack();
0 p1 d# f+ `& V# }7 k$ }...4 Q' d0 R0 H7 E, }% G9 i. L
Unbind(HttpContext.Current, SessionFactory)
# ~3 Y% Z5 d# z( |6 p5 m7 V' l$ H1 P% p. z) x
context.EndRequest += CloseSession;: f$ C' ]! D7 D, n* A# z
- t' A' h/ A8 p$ f 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|