 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what% t K/ s/ O& P3 w, ~7 ]% F1 v
基本参数:: ~; @, X0 o" G& o+ J3 n& R
jta | thread | managed | custom.Class
% L# P2 R$ z) {3 h) U5 K主要是配合
* T" P+ a2 n0 b8 {6 @$ g+ ~SessionFactory.getCurrentSession()
. r5 f1 c! }; p2 Y; x: }' H来用的.0 O& E0 h! o, O. C% Y, n
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,9 w% A% \ C% k& x
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
- R- u! A% D1 X" xcontext.BeginRequest += OpenSession;9 l# X( L* @! A
.Bind(HttpContext.Current, SessionFactory.OpenSession())
% j6 A( a' F* k) X- O$ s6 ]- N8 K- a4 V" @) a, o1 s
//do transaction' u6 c* K+ m( P+ f* \/ `7 {( W
getCurrentSession().Submit();7 n! s% c' I) v5 T( R
getCurrentSession().RollBack();
6 Y6 X. b& S4 R' ]...
G* [* f7 D' l! iUnbind(HttpContext.Current, SessionFactory)0 H7 \$ W9 L4 n6 O
5 `6 |3 m) O$ H, b- X! Wcontext.EndRequest += CloseSession;3 W) ^$ u/ K) S% b% [$ u% `
' C" k% E! ]6 n% \
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|