 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 C3 }0 p: s( t
基本参数:$ m' r1 E: N$ G
jta | thread | managed | custom.Class
" L# \- x2 A+ l3 ^0 l主要是配合4 c' h7 y$ J( }+ Q. O i. W
SessionFactory.getCurrentSession()0 ]( b; d9 [/ @* X' `3 Y
来用的., {7 x$ B' q. i' U# N
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
8 W0 J' o4 v |+ XWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: O! H( U% N- }* p, @3 _
context.BeginRequest += OpenSession;* R1 I# ?2 ]4 ^
.Bind(HttpContext.Current, SessionFactory.OpenSession())0 m* T$ L: l4 o8 U2 X B& S
/ o# V; |" Z9 k2 O
//do transaction
" a3 W# H5 i) l% U+ PgetCurrentSession().Submit();
* ]$ q0 ?& |5 _: b1 H; l' g( wgetCurrentSession().RollBack();# F3 w3 A+ v$ m/ s4 p
...! H, C6 k, O7 F) h& z: V
Unbind(HttpContext.Current, SessionFactory)9 P* [" ]: p* r s5 C# |0 E6 I
+ k/ G/ f0 [& S" q! w H
context.EndRequest += CloseSession;0 I( ?* m, Y# s5 r6 G6 y
6 s; B# `8 ~5 W& K2 }4 c4 L 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|