 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what+ L {3 r7 U' j% u+ v! F
基本参数:
4 F- @- @: ]6 d4 y9 L; c' Zjta | thread | managed | custom.Class
9 I, R M1 D7 g/ R# I* Y主要是配合& R4 d9 y8 \! B+ e3 h
SessionFactory.getCurrentSession()
v1 }# a' Z2 x1 w+ v& {来用的.1 l" w2 [! j+ i/ p- w- n* ?
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
1 Q4 ]# |5 B# a7 i: |+ RWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:7 y) [; J4 |0 c4 m
context.BeginRequest += OpenSession;" E5 c9 W9 L4 L8 k; b4 r
.Bind(HttpContext.Current, SessionFactory.OpenSession())
, z9 t6 _- K/ d& h J2 L8 \7 Z9 c* n+ M3 C
//do transaction6 \$ F$ A; Q3 h6 S
getCurrentSession().Submit();
5 \+ r$ \( U' cgetCurrentSession().RollBack();
" U9 x- l2 h" ]( M* j- q...
7 X1 ]8 B7 O/ o: f( K. y4 y( TUnbind(HttpContext.Current, SessionFactory)
6 C* w9 y C% h* d9 b8 t" o# ~7 J8 m" U$ O* r R
context.EndRequest += CloseSession;
$ V: p" n) X; H( t% G3 t6 K( I" v( F1 y' @
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|