 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
- E, E+ |7 h4 k' H' k基本参数:
( }; x& P! p7 ]: w" q4 n" }2 A& yjta | thread | managed | custom.Class
0 f3 y+ w+ s( W/ h* W- i主要是配合
/ b3 f4 i, L) Z- ?8 m% vSessionFactory.getCurrentSession()+ U" l/ h: l0 M* W( J. M/ w! f
来用的.
* a3 O# ~7 p6 ~Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
5 c: f2 m, ?! n( L5 t& h+ tWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:" R/ H! w, D. b' m* k
context.BeginRequest += OpenSession;$ {/ S3 A5 l/ i" v
.Bind(HttpContext.Current, SessionFactory.OpenSession())6 l6 P% y& }- C4 z4 F3 R+ D
1 g+ V$ M) _/ p, A1 B5 l/ s
//do transaction
1 l* Y4 f7 y" K- I- M. rgetCurrentSession().Submit();
& n- X/ C0 y2 V. P$ AgetCurrentSession().RollBack();4 K: M: K' `1 b; D. A3 V$ Q
...
6 d! Z+ J5 K3 w# X: s4 wUnbind(HttpContext.Current, SessionFactory)* B5 B+ _! q; }' M0 \( q
2 S/ }9 g9 X" _, D" R. @
context.EndRequest += CloseSession;: ] x. v6 V3 i# U& s3 K, k3 o
+ p7 r# x( G* y7 T6 ]( X& z8 R 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|