 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
. f2 W6 k! _# q [) p& T! a基本参数:
* F9 d/ k: t" U/ y/ F# L1 ajta | thread | managed | custom.Class
4 D. D5 B. [4 V/ `5 v主要是配合
4 {9 ]- P5 h) x; S; GSessionFactory.getCurrentSession()
* k/ w7 S; q9 v# l* _来用的.
w. ]( j- z2 `5 ]& @% Y2 C PThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,+ c$ {# U& v2 e2 c. O
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 f9 {" W; s0 K' C+ W9 E8 ycontext.BeginRequest += OpenSession;4 j: l+ A: X! o3 k) f
.Bind(HttpContext.Current, SessionFactory.OpenSession())
. \- j" ^8 X: l0 C( @4 ?# v
& n5 u4 F% }6 P# E- K7 m: Z0 x//do transaction
" e3 Y, |+ ^" q, e* h2 ygetCurrentSession().Submit(); q& Q* ^. F9 j M1 x, \& y h
getCurrentSession().RollBack();
! z$ `3 Z9 I- l% J7 j/ ]...$ c4 N* o% I: O0 w" @2 E
Unbind(HttpContext.Current, SessionFactory)
$ Z9 l" P4 y6 s" |1 Q( T! J: m" S( T4 @4 }) |- n1 e
context.EndRequest += CloseSession;' \1 ~1 \7 l1 b: A
+ I9 \0 f! b: s; s) G1 e 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|