 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what0 s4 ]. w/ J5 B+ B
基本参数:
2 j2 q: L+ A$ j3 \jta | thread | managed | custom.Class w3 I4 ?) ]& i2 [1 @2 V
主要是配合
6 @4 p1 x" i) v. G8 P' ZSessionFactory.getCurrentSession()% F( Y0 t4 r3 S G) p
来用的.- K2 r/ z4 N7 Q$ a9 |4 Y
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
6 h! f+ X6 k) ]Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
8 n2 ]5 t& u. i; R5 w' ]0 Mcontext.BeginRequest += OpenSession;
& j. w0 Y2 V% x# C.Bind(HttpContext.Current, SessionFactory.OpenSession()), U U# v) j% G8 o9 \- p
3 |/ ?: z, }- P- Z! n
//do transaction
_$ L* ~; V+ O/ F1 qgetCurrentSession().Submit();
! a" L' r/ F+ N3 ~7 PgetCurrentSession().RollBack();
h$ S0 H8 C2 H0 c9 {1 d m...9 m% ?6 }* J5 ^7 C
Unbind(HttpContext.Current, SessionFactory) t! p9 | G: B( `
1 B! n7 s/ E+ [* ^- _context.EndRequest += CloseSession;
5 U; f! W6 ?: t8 k6 H5 X( x( R/ I: r2 K
% E: B1 y/ X) O$ [ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|