 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what( C5 h$ Q4 V( z1 Y0 q8 V% B# F
基本参数:, P# b, x. p7 y+ M, S6 q6 E9 a
jta | thread | managed | custom.Class$ w, e8 M3 W$ ]! T. Z0 P- O
主要是配合0 x2 ?- b' E$ v
SessionFactory.getCurrentSession()
+ O+ g% ^6 i9 J8 m3 K: M来用的.
) M" z1 q# f: u6 X' wThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
* O% q. Y! z$ D1 g6 b5 c0 CWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
( T4 T: c! |9 e# ?4 \+ H- a k! acontext.BeginRequest += OpenSession;
! b' G$ w q* z) t8 U" f% d* P3 I.Bind(HttpContext.Current, SessionFactory.OpenSession())" u, }0 i; V q5 W6 ]; e% O
- w* k- N3 h5 g9 l
//do transaction
, t7 p- e8 {. ^* z3 ogetCurrentSession().Submit();
~: A+ M- W9 y2 `1 W# xgetCurrentSession().RollBack();
/ ^: j8 z# K. y/ w$ q4 j' Z...
% B3 R% F, j- J. o; I$ MUnbind(HttpContext.Current, SessionFactory)7 {2 a- S2 ]5 P5 h" o
* O$ O) H5 }: a' J% `
context.EndRequest += CloseSession;
2 q" ]- ?. [/ F: }% O) V
/ S: h( J- _* Q) ~ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|