 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what" P4 o* }7 e- k$ u+ e! W
基本参数:! b- m+ g; j2 t7 o( z4 R
jta | thread | managed | custom.Class5 F3 b/ T; n1 i& b0 L
主要是配合3 h! U; W. A$ X6 g; }
SessionFactory.getCurrentSession()+ Z( d9 V! W; q( d9 O
来用的.$ _9 U5 q" z, S% v) `* A; `
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
3 c4 K, F& Y, ^; y- OWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:) `* C" ], R& `4 _ ? y
context.BeginRequest += OpenSession;
* l1 R5 M1 j% v. S( h& _! `. G.Bind(HttpContext.Current, SessionFactory.OpenSession())
. z2 w c8 m( I7 ~2 c: V
3 t: U" Q3 F' P% W; T//do transaction
5 G" v" [ F2 {0 O& Y" ^getCurrentSession().Submit();- `$ v p( e- y' r8 E# y
getCurrentSession().RollBack();
- K) d- m" d C+ [; E+ M...
2 ]* e& w, C8 H# g3 wUnbind(HttpContext.Current, SessionFactory)+ v+ y5 N1 N! E8 p# ^) I+ [
- _% d& x3 O9 _* U9 n/ R' k5 K! v9 @
context.EndRequest += CloseSession;
3 G7 r9 i" O; o5 w! K; x8 W0 m7 S; Q7 v1 ?- m0 b
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|