 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what: ^3 M) k9 D2 l+ |% A6 X7 |3 m
基本参数:4 A' s) z; d& w$ K1 Z1 B8 B# ~
jta | thread | managed | custom.Class
! N2 @7 y+ B! F v* ^2 A4 ]主要是配合9 \ N/ q! I' }6 h# C( J8 G
SessionFactory.getCurrentSession()
, b1 A) F4 T) S% W% O. r来用的.) v/ ^0 j) w' J0 g X
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,! C( f. t1 ?" {% b. i- h( k5 e
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
; v8 U5 D6 a" k4 Fcontext.BeginRequest += OpenSession;
6 g3 {4 I% q; K Y0 S& q9 x.Bind(HttpContext.Current, SessionFactory.OpenSession())2 j" q& y+ s& P3 |% c& J
* p+ L' n; Q# r5 m: n//do transaction
, G8 A( o5 C4 F# o5 \getCurrentSession().Submit();
9 G% @- `4 U( C: qgetCurrentSession().RollBack();$ o1 o, O5 o# s( ^, C
...
' f- c% K, S. Z: V6 [" X6 sUnbind(HttpContext.Current, SessionFactory)
2 W! G, I7 V \& E4 v, w7 G" w. U) w
context.EndRequest += CloseSession;
: F& l. m! B' V$ k1 F
! T* b) [! e) {/ o# R 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|