 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what6 Y k; h! z9 G" H, V0 i- w, C
基本参数:) L+ G1 t$ H- Y/ A8 M3 B
jta | thread | managed | custom.Class! T% X& ?" m# ?8 e
主要是配合
2 b. l9 l- m% q' ?) gSessionFactory.getCurrentSession()
: y, @- M2 o$ D( b/ v; f1 t) I来用的.1 n3 q" ?' ]" o0 ]+ D7 E. Z
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,/ C/ P* k* j. }' c. a5 R
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 h9 f( j8 n3 Z9 F' V8 ccontext.BeginRequest += OpenSession;
6 J: [4 ], y# T6 `.Bind(HttpContext.Current, SessionFactory.OpenSession())
' s$ S$ }6 i3 L
0 f% Y" @3 g: T6 u: a0 L4 l t//do transaction, A7 z3 ?3 Q. g* K) r, ]
getCurrentSession().Submit();6 Z+ L' K4 s0 G6 t; j; ]: V# n& C
getCurrentSession().RollBack();# n& F2 d6 S9 }( u3 d8 x# j) v W& d
...
( z& p; B1 @/ D( oUnbind(HttpContext.Current, SessionFactory)
/ k" d) z' m: x. g& k0 @/ \7 Q
' w9 A6 w( m# C- z: |# `. { ?1 n1 `context.EndRequest += CloseSession;
# n9 f2 K/ i% Y9 v7 s' N, }( G/ r4 V+ f% t1 z
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|