 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
' @1 E! s+ E5 n基本参数:
* |6 r9 p' @' c: z" D1 g5 @$ Zjta | thread | managed | custom.Class
+ e% d/ \! V! G3 p主要是配合7 y( M6 a4 ?: h4 ]
SessionFactory.getCurrentSession()5 R) l4 i, r& f) ?
来用的.1 O' ]% w9 v, {
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,+ K5 S" s5 J4 U' j* X `
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 d5 |! Z/ T- l& h; G$ i3 `" q8 k: L
context.BeginRequest += OpenSession;& |! k( O& W; ?2 E; ]2 ^8 ^8 n
.Bind(HttpContext.Current, SessionFactory.OpenSession())" h0 x* V) N/ [3 J9 Y5 \
( r5 l+ t+ C: I) h3 n
//do transaction9 X2 }( J. L8 Q$ t# J
getCurrentSession().Submit();5 ^0 L. w& D1 A6 o
getCurrentSession().RollBack();
& p! F3 l! u) \" f. ?7 k..." Q3 J1 m6 |3 X1 T& S: t
Unbind(HttpContext.Current, SessionFactory)
0 A, F- l4 k$ {2 a e+ D* g) {
% [9 W4 `! o" O8 Wcontext.EndRequest += CloseSession;8 A% \0 e+ a. }4 C+ G2 `
~9 {3 M2 _9 \3 Y5 U( g# h
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|