 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
2 G* x- |& `$ w基本参数:
7 o4 t8 y# [5 L- t8 s! yjta | thread | managed | custom.Class: V' V# R& R6 N' N/ K. C% Y' Z
主要是配合' c5 q: s' Q" Y E) z( q/ M3 U+ g
SessionFactory.getCurrentSession()
! |7 C* k( B+ h: b7 {0 P来用的.
: o5 R& f1 ]$ K. p, T' n7 i0 {Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
8 h1 [' M6 y0 P K% g/ ^) c) pWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
. u: p& p+ Y0 c) Ncontext.BeginRequest += OpenSession;7 ^; _; C1 M. @' J
.Bind(HttpContext.Current, SessionFactory.OpenSession()): N! D4 q4 ?5 c9 q5 e
6 V T4 [$ Q. m/ w. A1 Z8 A
//do transaction; a" f5 B3 P7 a% Q5 y
getCurrentSession().Submit();1 F @1 x; [" G+ G! Z9 _- C
getCurrentSession().RollBack();
5 `9 |. `) V, I- k+ V+ L5 z/ @...
% l4 `3 x$ r6 S! KUnbind(HttpContext.Current, SessionFactory)
6 ^2 w" a! H. v; X9 @0 L. L9 `1 |3 i' \+ Z
context.EndRequest += CloseSession;
; \8 m% b3 R5 y$ f9 W5 a: O1 X
, z( I; K/ e/ P% | 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|