 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what r: }* @+ E1 o! t+ M
基本参数:: o) S, J& P3 A. e, w
jta | thread | managed | custom.Class" h9 y( K' W( i
主要是配合' U* k( n9 S/ F) b9 j, C
SessionFactory.getCurrentSession()
) f4 d+ K, Y% D+ c! P$ l来用的.# `; C$ p- d$ p' ~- [
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
+ B- C' u" g; v& _Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
7 K" ]# ]8 B- I- ycontext.BeginRequest += OpenSession;
r5 ]: Y" n' X( I.Bind(HttpContext.Current, SessionFactory.OpenSession())
: U7 X7 u1 h( F: }8 o' w1 p2 e) L( J% Y6 {. L7 i/ @
//do transaction; I+ @. }. [* D1 k6 S
getCurrentSession().Submit();4 b. m7 ]' Z: A. v1 X6 h
getCurrentSession().RollBack();. \) a8 n5 q8 c) f) X- p
...
1 q$ A/ m" c2 X% m. D- ~7 M& gUnbind(HttpContext.Current, SessionFactory)
) N5 J: E* P' d6 D# S& W8 q7 w9 b+ w" ~" s9 n
context.EndRequest += CloseSession;# X s& V4 P& P% j) x0 }1 L/ c& c) O7 J
/ B6 Q- q4 S* @3 |! Z 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|