 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
- y' z5 H I7 M( M/ D' q4 T8 x8 }基本参数:
3 Y f6 r9 E& _1 F" qjta | thread | managed | custom.Class% l1 k: K3 o* a% L8 Q1 J& a7 U
主要是配合4 ]0 j& [- T0 R- Z! ~2 a
SessionFactory.getCurrentSession(): \( F1 u. e: q% c8 `8 f
来用的.- J8 d/ q3 Q5 O# D; J/ f6 L
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
" W5 _% d5 x5 m* p, X, V& p/ m% wWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:! {, |+ a Y$ \
context.BeginRequest += OpenSession;6 t3 l! ^5 Q0 n0 A' v# {
.Bind(HttpContext.Current, SessionFactory.OpenSession())8 W; _1 U. {5 `4 Z0 g3 x, z
2 Z" Z) L) N) ?8 i" E" T9 X
//do transaction
. U4 c$ ]+ b- Q: Q( A& tgetCurrentSession().Submit(); O% s2 M: P- \. l: w2 b) d6 Y0 r& q# g
getCurrentSession().RollBack();
$ m6 f" a1 _7 s; }...
( p4 G+ W1 j |. ZUnbind(HttpContext.Current, SessionFactory)2 M0 \ ~) u4 l
8 M$ d5 O* g1 B S
context.EndRequest += CloseSession;9 q x; [& H1 t; z, D0 O- x+ K
6 k W: j$ s) x; N 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|