Do NOT hold static session in nhibernate
2012-09-06 10:28
239 查看
Refer to /article/5064168.html
Every data access strategy after server-side cursors has had as a goal minimizing the length of time an application holds open a connection, and NHibernate is no different. Therefore, having a static Session sitting around open and available is a poor strategy.
Instead, you can use disconnected Sessions. You can still create a shared Session object (either instance-available or truly static) for use in your methods. However, instead of leaving it open, or closing it in every method, you can follow the following pattern:
Every data access strategy after server-side cursors has had as a goal minimizing the length of time an application holds open a connection, and NHibernate is no different. Therefore, having a static Session sitting around open and available is a poor strategy.
Instead, you can use disconnected Sessions. You can still create a shared Session object (either instance-available or truly static) for use in your methods. However, instead of leaving it open, or closing it in every method, you can follow the following pattern:
public IList getClasses(){ IList classes = null; try{ // session is declared in instance scope session.Reconnect(); ITransaction tx = session.BeginTransaction(); classes = session.CreateCriteria( typeof(UniversityClass)).List(); } catch (Exception ex){ // handle exception } finally{ session.Flush(); session.Disconnect(); } return classes; }
相关文章推荐
- (详解)Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session
- effective c++ 条款9 do not call virtual function in constructor or deconstructor
- flex error:HttpFlexSession has not been registered as a listener in web.xml
- 解决:Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into Flu
- currval of sequence "follow_id_seq" is not yet defined in this session
- Google to Android Developers: 'Do Not Develop in the Open'
- The entitlements specified in your application’s Code Signing Entitlements file do not match those s
- currval of sequence "follow_id_seq" is not yet defined in this session
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM
- The entitlements specified in your application’s Code Signing Entitlements file do not match those s
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM
- What will happen if we begin transaction in hibernate but do not commit it?
- PRB:WebGrid column headers do not align with their columns in NetAdvantage for .NET 2007 Volume 3
- Formats of input argument do not match() in function cvFilter2D cvfilter.cpp
- Comics: do we know that we are not doing research in the wrong way?
- The entitlements specified in your application’s Code Signing Entitlements file do not match those .
- iOS真机调试报错,The entitlements specified in your application’s Code Signing Entitlements file do not mat
- How do I tell if a regular file does not exist in bash?
- The method load(Class, Serializable) in the type Session is not applicable for the arguments (Class<
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM