 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; G* T( Y" r0 x$ X, T基本参数:
; W( n% P2 {* S$ x8 jjta | thread | managed | custom.Class9 S# {, Z" N9 G% n) K; \* J/ m; S( ~4 Y
主要是配合% q- u) n( O" Y6 _# m# `
SessionFactory.getCurrentSession()
- `0 r! _. G; w' }/ A8 R, R* S来用的.( T& t/ [" b" m' j: ?
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
1 |( P$ G8 o4 S1 ]; c. OWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:( S7 X7 g' ?5 q
context.BeginRequest += OpenSession;3 W4 Z( w' @; E' a: G! A. h$ `# |* f' |
.Bind(HttpContext.Current, SessionFactory.OpenSession())! `$ d& p2 D, I
n* k& I6 f. L9 V' Z9 F3 D$ ~& _//do transaction
6 D; F5 U2 @1 v/ fgetCurrentSession().Submit();
# H H) I* @- n, Z/ m- xgetCurrentSession().RollBack();+ n, e# W9 p' |* o0 D/ w% I' P& ^" Z
...
& n1 P8 J; ^' G* h8 t7 O4 v1 j: ?$ zUnbind(HttpContext.Current, SessionFactory)! l f& a+ L3 a
( R9 y. j# }1 n5 Z
context.EndRequest += CloseSession;
& j. P% B) B1 q, _ t8 Q% u8 B0 W. i7 D0 X, V
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|