 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; o8 A6 `0 n. p8 U9 Z2 O6 m基本参数:
* Q2 r! m, ~) \# X2 l6 O# w% Z) j# ljta | thread | managed | custom.Class
2 D! `( C; `/ I3 i4 p1 d主要是配合( \1 s' O0 Y1 o- q
SessionFactory.getCurrentSession()
7 e$ Q6 t3 D. W: h来用的.
* F4 P9 \4 M. pThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,8 n7 ?% P, c* v0 d
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: i' W0 G: H" p+ i+ N
context.BeginRequest += OpenSession;1 _" w7 Y( ^7 {7 V
.Bind(HttpContext.Current, SessionFactory.OpenSession())
* {9 B- M& r8 O" @+ f) ]
* N( m! E6 K! t+ S. _//do transaction
6 J+ n5 B+ G/ k. ]9 VgetCurrentSession().Submit();2 S$ W, M/ N" r) u
getCurrentSession().RollBack();4 D/ F# R/ k Q( X" E
...! R" J0 f' }- s% k) X
Unbind(HttpContext.Current, SessionFactory)
; T9 {6 N7 C( g4 a$ P+ w w8 v9 o5 P+ { a6 _9 t* A
context.EndRequest += CloseSession;
6 X2 v3 W" x% q9 b2 i: g
u5 j+ p" E7 o 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|