 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what2 `2 Q. ]& c$ | E, z0 T/ M5 y( |4 \% E6 {
基本参数:. @% P* V% V2 d4 V5 f0 e+ l, Q4 B
jta | thread | managed | custom.Class
% m" R0 p( S$ `8 l; @. E7 g主要是配合
* y$ s3 L9 `0 J9 ]! C" vSessionFactory.getCurrentSession()
$ S. E2 J- ?' w+ ~: x9 t来用的.& @& N$ s; B0 M
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,; m: Q, x. [* {4 M# l
Web application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如: u* v1 @& j$ u6 V" J4 W, a3 Q% b, r
context.BeginRequest += OpenSession;6 e: q. a) f) N. p+ ?2 P; B. y9 F/ [
.Bind(HttpContext.Current, SessionFactory.OpenSession())0 V8 O0 I, h0 X' S5 E- c
% | s; Q* v% \8 Z8 v0 V
//do transaction
) m7 e: \8 D* y0 X. s" t/ }getCurrentSession().Submit();7 z- Q$ R; I- K4 q
getCurrentSession().RollBack();
) O. N5 I, @7 U. k2 u...% ~( \& z& d, I7 P3 i' i
Unbind(HttpContext.Current, SessionFactory)$ |1 ~6 n& C" |& [, E0 ?
" {1 G* \, N, p% A
context.EndRequest += CloseSession;& r4 R _3 H0 g+ X9 I
3 Z0 ~/ i8 M1 u' P 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|