 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what* P) `- x: T- _1 S% A7 p, l+ n
基本参数:
& m# S8 m& G6 }" ^jta | thread | managed | custom.Class
% {) U& F h2 a8 p6 [4 Z7 {主要是配合2 P8 h2 e2 \2 ]+ [
SessionFactory.getCurrentSession()7 z, d( M: R8 h5 X) l2 C
来用的.
8 s- Q% ]5 V, o8 s* P2 J0 r, m( JThread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,: T4 P0 V8 N! J' j/ c
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:1 {5 @1 L/ R, O; e, V
context.BeginRequest += OpenSession;/ R1 v( C& U; w: U0 C/ S
.Bind(HttpContext.Current, SessionFactory.OpenSession())4 `3 p! W& R1 D
`* ^5 @: I( X9 }) u6 l//do transaction" {0 H, `, d' @7 Y7 T) y1 ]
getCurrentSession().Submit();8 O! e* ^& t$ N* q
getCurrentSession().RollBack();
. V8 b" C6 ~3 e9 t K k. L...3 B L+ X* ]4 L8 Q2 N- `3 A
Unbind(HttpContext.Current, SessionFactory)# |& Q5 _+ Y) `1 g) T N2 J
9 @8 u2 B: R$ f" X* _
context.EndRequest += CloseSession;+ L: T! d% I" ^, \5 I
& U; H/ p, t2 G$ C, e0 s: r0 y
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|