 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
, I; H- J* J L Y, W基本参数:4 o2 ~& }8 \* `- ?
jta | thread | managed | custom.Class
0 u# |) D9 Y. o2 C4 l( p s. [主要是配合6 z8 y+ ^3 v. R4 W+ ]
SessionFactory.getCurrentSession()
9 @$ D# `& ]8 ?来用的.- k( ]1 C3 F, R2 g& Z6 u" }
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
5 D) V I7 r* i \+ x/ A( }Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
3 X! i, O! n0 _3 ]$ Mcontext.BeginRequest += OpenSession;
- j# b* ^! Q) s8 G3 i: Z.Bind(HttpContext.Current, SessionFactory.OpenSession())5 [; C/ p5 q- Y1 m$ A; [
" ?1 `% e% _- X2 a//do transaction
2 v0 _, z% H- Q: hgetCurrentSession().Submit(); N- W% W: I1 Z* K( z( E" @1 L
getCurrentSession().RollBack();( t; N4 Z, {4 ~
...
_: x) H ]8 o" {' B$ U( x+ n1 JUnbind(HttpContext.Current, SessionFactory)
* H: x" x/ n+ j, k( j5 U' U, Q7 {1 {% g% |) G5 i
context.EndRequest += CloseSession;
9 V% w9 K7 R& p5 q+ J: |% @* O2 p" j, b9 q. k0 h1 r
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|