 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; u# ^" n1 n* [- k6 T) {基本参数:
/ u' v: _4 C8 w4 a; {5 sjta | thread | managed | custom.Class
8 ]/ V4 e9 Y# Q8 C" J5 E主要是配合
1 S0 `% t4 \ |% i! B# PSessionFactory.getCurrentSession()
% Y' n' A5 H( i3 t来用的.
7 K$ Y( i3 `6 W: c3 }Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,* |. o1 s3 q) o8 r$ @+ T1 l: z/ [
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:9 `! H) ?3 B6 x4 ]% Z
context.BeginRequest += OpenSession;1 j5 V5 l z) r' U4 K$ R$ ~
.Bind(HttpContext.Current, SessionFactory.OpenSession())
$ ?" m: S. J: `( g% Y. F7 [9 p. n& W9 T I2 H0 p
//do transaction! ~: c, m9 `6 G
getCurrentSession().Submit();
@7 Y: ]) v3 ?: N% a: i; dgetCurrentSession().RollBack();
+ L4 }' d8 g) m7 D...- z# ?) U* ^: Z; O9 w! T) E
Unbind(HttpContext.Current, SessionFactory)
8 x* h, k& k+ c4 s" {5 z. p: O1 l! j1 ^9 n5 ]
context.EndRequest += CloseSession;8 _; f- F" Z& E" i) y: p
& q' ]( o7 h" T, e4 j, u& f; q
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|