 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what1 d% U$ x( \6 D) z4 {
基本参数:
* {4 I. i/ Y0 P7 R4 fjta | thread | managed | custom.Class
3 k: F% ?- O! L$ Y7 Q5 ?0 `主要是配合4 k' Q& a+ O9 J6 j; E
SessionFactory.getCurrentSession()* `: j& N% O6 [4 {1 f$ v( T
来用的.) S- s* o/ r" w7 c% a) g, k: c
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, @# @0 Y" |/ Q0 R; X
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
! b( L% Z, d8 [8 O1 Pcontext.BeginRequest += OpenSession;
7 i) l; L, F ~7 j.Bind(HttpContext.Current, SessionFactory.OpenSession())
" B7 C! R" `/ e: ?8 F* C) L- e* H" H
//do transaction
: I% n; o/ {9 x5 _, G1 qgetCurrentSession().Submit();
c% N( W" _* i5 f8 H, ?; z3 hgetCurrentSession().RollBack();
* B) N( ^# n) ^... a9 C& m8 M2 i. \
Unbind(HttpContext.Current, SessionFactory)
5 h" ^5 J# T* P7 E _% b4 F
; }2 N8 H; R" econtext.EndRequest += CloseSession;
0 |) H# Q6 J) g# S' P& }& J- w' t& i, l# j1 o
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|