 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what- V- S0 |7 B1 z; Q$ k
基本参数:
# u5 w( q. p+ N8 L0 tjta | thread | managed | custom.Class1 v- U7 x: C u7 z3 ^1 C
主要是配合6 k3 ~* t& S8 K5 r @
SessionFactory.getCurrentSession()
4 \4 |2 l% K A& B/ Z, S5 M来用的.$ i6 e8 g* t. C* y, S- E% x" R
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,$ Z: U$ U* E$ M3 T
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:5 ]& y4 c2 Q. R" G, t
context.BeginRequest += OpenSession;" ~3 g# O- y, p% h
.Bind(HttpContext.Current, SessionFactory.OpenSession())
4 v; ^, W! C! s- B/ ^4 M$ Q$ a
4 q1 T. f6 ]' l* i//do transaction
1 `$ x( ?" W+ C8 Q0 ~1 Q" z% ?* j6 |getCurrentSession().Submit();
) B9 s, x: I; V/ S2 ]getCurrentSession().RollBack();
# X. L' n" \- l...+ q, l) }, q- r4 \& e
Unbind(HttpContext.Current, SessionFactory)' A! r4 a8 w5 x
- B3 s" M$ I0 h( `0 Q, p! U
context.EndRequest += CloseSession;1 o4 P5 v( t2 g* L7 h& T9 Q7 L
6 m' o f# V2 T- L# f+ Q$ l 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|