 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what0 m+ ^9 k' n" C) G# M
基本参数:' \4 W: o+ d2 Y" X
jta | thread | managed | custom.Class
( N( e' j5 a' W主要是配合
9 u4 f0 _4 R) ~. _, ySessionFactory.getCurrentSession()
" h& @/ p. K+ [2 q8 K% h来用的.% n4 m" q) L8 |, b7 S
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
3 S! @$ ~7 K9 F, t6 x4 Z7 OWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: H% |- H w2 b; w5 E* K3 @6 @7 x
context.BeginRequest += OpenSession;
; a9 l5 K: ?' a) g( @.Bind(HttpContext.Current, SessionFactory.OpenSession())
1 n4 ^% k% e b4 T* X- C2 t# @3 {3 S _2 I F3 j
//do transaction
+ F) O# K8 C( t# VgetCurrentSession().Submit();
! g) d. v @5 w+ j" s% a. ^getCurrentSession().RollBack();
' p- E/ B- l1 L8 e5 Z5 g3 t...
5 B7 e" T4 e- |7 n- q4 h q- W; d1 N( sUnbind(HttpContext.Current, SessionFactory)
* ~& u# C$ i( R* M- F
; A8 u+ e4 P0 Y5 w r* V" o3 Icontext.EndRequest += CloseSession;
% p3 ? ~6 X: _ g1 Y4 r$ A8 d( s
" N2 u! _& i% O$ s1 H 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|