 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what* R8 @+ C( r: G9 f
基本参数:
' J& B+ } v4 Y! d* P9 y. Qjta | thread | managed | custom.Class
& V; }$ X, ~9 L' I& {% e主要是配合
* W7 w8 C5 O/ jSessionFactory.getCurrentSession()
" P; p4 B2 v5 K! F来用的.1 X. A ~) h6 \5 c9 R4 X0 e8 |" x
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
, E# N; I+ o( Z# |6 O- ^8 F4 z( ?/ }Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:) J, |$ n9 E7 B" Q2 j! Z4 G n
context.BeginRequest += OpenSession;
3 {% o: n) U2 l.Bind(HttpContext.Current, SessionFactory.OpenSession())2 d% m" \& V! t1 h7 j- ^
# V4 `. _. I# N7 j//do transaction4 X1 y( y0 R G3 r( }6 Z m5 z% x% K
getCurrentSession().Submit();
, S. S }4 V0 Q: P1 v. ggetCurrentSession().RollBack();
+ x: l' | j( u/ Y& c% g* s...
7 R# K& d* a4 O' wUnbind(HttpContext.Current, SessionFactory)' P: b: [- L* b3 R+ B
2 P. x% L9 f0 A, C% w4 n) J9 ^. B! Acontext.EndRequest += CloseSession;! Z' ^* L, h; g
9 u4 W' }+ M& q& W; A4 ]1 V6 K4 C
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|