 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
0 b2 m+ \8 W$ W5 b( y基本参数:
) ]9 d3 y3 O. `jta | thread | managed | custom.Class
' s- x8 Y$ K( B# c主要是配合. o- b+ W! H* U& |9 z
SessionFactory.getCurrentSession()
; X) }4 c2 |* i( b来用的.2 ?7 r( s8 E9 m8 Y
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,+ H4 b& V5 }5 W' Z+ d; {+ H( g5 W
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:% x7 M6 k0 i) ~$ q8 k( g! z, H5 Y/ @. I* a
context.BeginRequest += OpenSession;
0 `# U O& M2 O/ Q- p$ j.Bind(HttpContext.Current, SessionFactory.OpenSession())
4 V- u" C. [7 N( w6 ]1 H
/ k4 n5 i: E" M- n5 q//do transaction' x/ ~; T$ a: x3 E
getCurrentSession().Submit();
+ `4 m! u. x, j7 C) SgetCurrentSession().RollBack();/ g( R g8 V; B6 u5 P' x. o5 s
...6 ~% o: z. J: ~
Unbind(HttpContext.Current, SessionFactory)6 [; E) @3 I& Y0 W9 B# Y
# k0 P& G. L5 k$ P* ?1 ^) U( [
context.EndRequest += CloseSession;
- o& i3 ~' M3 w# m0 _3 b9 C% m& s0 L' Q; \; [7 T* M
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|