 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
0 _1 d: E4 ^0 I0 o1 x8 w* o* j6 \7 y0 w# ?基本参数:
$ T# _ {, _& \ d% jjta | thread | managed | custom.Class
2 w: r2 W; g5 M. F+ o8 I主要是配合/ c( N5 [' l E, R% R* ^
SessionFactory.getCurrentSession()8 q/ k4 i6 n5 Q0 v; ^! G
来用的.# `$ g' Q) u& p: v3 u8 C6 J
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
: ^# H) q% k: X2 P& V4 UWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:' N: n# x, U+ a( s& w
context.BeginRequest += OpenSession;
7 G$ d9 [ t8 f/ }.Bind(HttpContext.Current, SessionFactory.OpenSession())
, B- v4 y& F, t5 N' {1 z# d/ c | N# p" W4 |$ f) [
//do transaction
4 L- z4 W; T* T0 L2 p7 E. }# Q: ~getCurrentSession().Submit();
0 J% m; N b5 B/ jgetCurrentSession().RollBack();
& A, F. y0 @9 d% B% U1 e7 ^...& n; v- r- k8 j0 {8 p
Unbind(HttpContext.Current, SessionFactory)! D8 r5 N! V1 f
! B/ o, s3 n% T0 B \" ?: D6 K* dcontext.EndRequest += CloseSession;
5 e0 Q$ S9 k+ e9 \, r$ O
- R) Y3 L* @7 Q6 n2 d3 ^ 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|