 鲜花( 2)  鸡蛋( 0)
|
current_session_context_class 主要是用来配置当前 data transaction session bind to what
; F* `& C# m9 [基本参数:! H& q: @$ | F/ t6 H
jta | thread | managed | custom.Class
2 h& r t8 z# I1 _$ J! k! I主要是配合
- y I/ v, J% _4 R S* lSessionFactory.getCurrentSession()
* F+ }1 ?( a- W" D9 R5 ^来用的.; o. p: d$ ]9 z3 U! }
Thread 指自动绑定到当前进程,managed 是在有些managed environment 如果使用 container-managed transactions (CMT) , 需要自己bind 和 unbind,这样 getCurrentSession() 才可能返回正确,
- k# [0 u- r- n$ T" o; |" R2 AWeb application 状况下,每一个HttpRequest 都是独立thread, 所以可以在BeginRequest的时候bind, 在EndRequest 的时候Unbind, 这样就是可以使用getCurrentSession(), 例如:6 r3 L9 C: F( i
context.BeginRequest += OpenSession;* K$ D% u! R+ B0 i
.Bind(HttpContext.Current, SessionFactory.OpenSession())
" m$ _# w' \- ~/ W3 J8 \8 J
/ m( M, U2 J2 E//do transaction! u, D# H/ n" \ a5 M6 _ x
getCurrentSession().Submit();) F' i) K. r/ ]: s8 _
getCurrentSession().RollBack();, r( `2 I3 |: k4 y8 [: \, e' Y( l" ~. w
...& \( o" g8 [3 q) v4 I2 Z9 z
Unbind(HttpContext.Current, SessionFactory)0 O+ l v7 `6 ?5 `5 l
' M& Q. r& g5 x6 v Y _context.EndRequest += CloseSession;
% V7 N# @) r0 J) {6 Y
2 ^0 \ J8 P R! b8 X% b 如果配置不对,getCurrentSession() 会出错。但是不会引起application 不稳定,查查别的地方吧。 |
|