 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
! K Y) l4 @- [7 v x3 d9 {( Y基本参数:5 L; e$ j; B L% {4 j. g/ F3 }
jta | thread | managed | custom.Class' B& q0 z& H$ w6 D1 d5 H+ K
主要是配合( b8 Q s+ ~* q, A/ S" m" U
SessionFactory.getCurrentSession()
% D4 A6 `' {/ |9 i来用的.3 d+ H2 ]2 ]. ^9 U6 D
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,) J0 d' x: g1 k4 x
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:
6 l9 {1 a8 a I% g# W( K% Q% A1 Icontext.BeginRequest += OpenSession;2 N5 B) T! R# a5 f/ [7 Y4 H" C
.Bind(HttpContext.Current, SessionFactory.OpenSession())
( G2 @* X: Y2 g, T0 p! f& |
+ R/ c. b! i9 k- b//do transaction
* |6 [& ?3 c, C3 v1 i4 qgetCurrentSession().Submit();+ ^5 T% t/ j G' Z% l
getCurrentSession().RollBack();% g3 C8 v$ Y" g1 p
..." b) i% [- u" U9 }1 q+ _+ y
Unbind(HttpContext.Current, SessionFactory)3 W O4 V- O( a) [! \ C
. P2 s- {3 U- ?3 y+ Z2 ?+ \. E
context.EndRequest += CloseSession;
; C6 ?* N; @' a. x% j; S( S) H" H5 W' U1 V1 r
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|