 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
/ u( ~; z: K4 e* A基本参数:
, B: {0 K# F r7 G+ m2 m1 E% {jta | thread | managed | custom.Class" |8 J+ F4 T; [
主要是配合
+ N& C0 i3 |+ J! pSessionFactory.getCurrentSession(). I! {" w- w. E5 D
来用的.
4 g% b- Z. M! M5 k7 z# M+ cThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,% k5 [7 e' Y5 v0 I
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:2 L) ~2 ]2 h7 {: y# f9 D7 {6 @
context.BeginRequest += OpenSession;
2 y0 A8 {: L4 b/ f) y! _.Bind(HttpContext.Current, SessionFactory.OpenSession())9 _ }& _* Q9 R9 l) D z. o
- P/ y( O' C; y4 x! f8 H
//do transaction
" S) r5 s1 U8 V) ~+ Q2 S6 IgetCurrentSession().Submit();
: ?, T- P! T" s \% bgetCurrentSession().RollBack();3 e H- g- ?+ a. H0 L
...6 e. r Q# |6 n8 W# d3 `" ], i
Unbind(HttpContext.Current, SessionFactory)
% t) j9 f3 O2 L5 n4 d# U, ^- {3 k* E2 M* {% D+ C
context.EndRequest += CloseSession;$ H0 w' [; c! f( R8 u3 b4 [; D
7 }; f3 Y; O) T P( U' n [0 t 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|