 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 h* k# K2 o6 q1 T" D+ \, [+ K' S" K
基本参数:
& ?% J+ w, M" }6 xjta | thread | managed | custom.Class. n. H" K- H2 |3 ?1 {. ?$ n$ [- {
主要是配合
3 Y$ V0 T3 o- z+ O% t. w% nSessionFactory.getCurrentSession(), x- B9 @- i4 }3 S. v! A
来用的.
) R3 [: o' x, }Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
8 X$ V& }5 Y% s8 P: G3 j! M9 }Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:8 v5 F1 k$ m9 ] k9 y1 B
context.BeginRequest += OpenSession;
4 F) | {4 }8 n+ T5 S- y% S; ].Bind(HttpContext.Current, SessionFactory.OpenSession())
7 r* g; Y+ d+ d3 B
3 E' z; o7 l# s; ^7 s# |//do transaction0 {! E$ r" k. y) ?, G
getCurrentSession().Submit();9 N4 Y: j. x" K5 G; E* d9 \
getCurrentSession().RollBack();8 l/ C, D2 F7 a8 T! _2 b
...9 X) X9 c: ~# P0 |, I* f. i1 c; s! Y
Unbind(HttpContext.Current, SessionFactory)
: Q% t Q8 u8 v5 W# k
) D/ I' ? c: {context.EndRequest += CloseSession;2 a8 P+ W" t! I \2 P* v1 B
2 s/ B4 n; Z' y* K 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|