 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
+ t L1 X0 b7 O( i6 T2 Y# w基本参数:
5 R6 B- ^& D& d% g( hjta | thread | managed | custom.Class
( e9 m- x: G6 S$ {7 K! i5 ^主要是配合7 u" O) n4 o% m$ `2 i
SessionFactory.getCurrentSession()
4 t1 C: {1 H, ]. K i4 A来用的.
8 l% g! h& z7 I/ W6 ?& Z" MThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,, P; \' M9 ~5 E1 @9 t( \! X9 p
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:4 P, ]: H: {! d, H+ [" f5 z. _
context.BeginRequest += OpenSession;( s5 q/ B: A8 @& w
.Bind(HttpContext.Current, SessionFactory.OpenSession())" Q) {9 i- L* e
# W" P; f" A) J1 W" e# s& E3 F
//do transaction
& o* N% o! ]7 `8 IgetCurrentSession().Submit();
( H, C2 g4 x, o0 Y, Y, `getCurrentSession().RollBack();
1 [5 `3 q& b$ N% i% S c- i..., l1 S* h" c+ U, z
Unbind(HttpContext.Current, SessionFactory)4 E+ d2 ^4 h( I/ A) l- A
; Y4 N0 E# G7 _. B1 ~- u2 j+ _0 {$ n8 fcontext.EndRequest += CloseSession;8 d- l. o4 ]9 Y. L
# q8 _+ Y+ I" Z0 g: u5 Z1 h
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|