 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what( P* r5 G+ g4 |
基本参数:$ g8 A( H; Y, I3 N% b T: T" L
jta | thread | managed | custom.Class
5 a: w5 Y/ Q; P& T" a5 @主要是配合/ K1 Z' X4 G9 p6 _
SessionFactory.getCurrentSession()9 a1 B7 [$ U$ @& l
来用的.# p& U- R9 ~) u7 u3 u, _
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
' t) c5 \% b! r3 |. ]+ f! {% ~6 EWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:6 U# I8 h& [/ K* J
context.BeginRequest += OpenSession;
O/ V' p1 V; P/ A& e5 L1 R1 n.Bind(HttpContext.Current, SessionFactory.OpenSession())
- E F6 q1 y: _3 P. F/ t
9 T8 Q5 t" A2 {* y& M; V) D//do transaction
7 K: @9 n/ T3 i$ w6 [& hgetCurrentSession().Submit();
+ Z2 F2 x# s9 B8 V; p1 P" bgetCurrentSession().RollBack(); t# S# A7 ?6 ~
...3 Y0 T9 y8 X0 t2 u) d; j6 s% ~
Unbind(HttpContext.Current, SessionFactory). ]2 W/ ?$ O% ?* C* E
3 H% R: q M$ J$ d$ Y6 Z1 [: |context.EndRequest += CloseSession;/ g8 @: `8 B y( z
6 Q3 H) {' v& l4 E2 @& W2 g
如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|