Thanks Oskar, 1) Do you mean if I use singleton, so I cann't use multiple threads? 2) If I want to fix "Session is Closed" exception, I must be let session to open. and I don't know when load the lazy properties, so I cann't dispose the session, in this time it's happen exception when I load other entities.
Regards, litgle. On 5月23日, 下午7时34分, Oskar Berggren <[email protected]> wrote: > In your design , you need to take the following into account: > * The session instance is not thread-safe. It is not intended that a > single instance should be readily usable from multiple threads. > > * The session represents a logical unit of work. It is not intended > that a single instance be kept around for a long time. In many > cases, the session lives only for the length of a single transaction, > though other patterns are possible. Keep in mind that the > session keeps a cache of all loaded objects and will grow in memory > and time consumption (due to excessive dirty checking) unless > recreated fairly frequently, or Evict() or similar methods are called > to empty the cache. > > In a web app, a common pattern (open-session-in-view) is to use one > session instance for each http request. In desktop apps, one > possible pattern is open-session-for-each-dialog. Depending on your > architecture, even your desktop app may be focused around > a "service layer" through which all calls to business operations are > routed, in which case one can explore using a session for each > such call. > > /Oskar > > 2011/5/23 litgle <[email protected]>: > > > > > > > > > Thanks for Ciao, Oskar > > > My DAO code as follow: > > > SessionManager.cs > > =========================================================================== > > ============================== > > public sealed class SessionManager : IDisposable > > { > > private ISessionFactory sessionFactory; > > private Configuration cfg; > > private ISession session; > > private ITransaction trans; > > > public static SessionManager Instance > > { > > get > > { > > return Nested.nHibernateSessionManager; > > } > > } > > > private class Nested > > { > > static Nested() { } > > internal static readonly SessionManager > > nHibernateSessionManager = new SessionManager(); > > } > > > private Configuration ReadConfig(string connstring) > > { > > ReadProperty(connstring); > > ReadMap(); > > return cfg; > > } > > > private Configuration ReadProperty(string connstring) > > { > > if (string.IsNullOrWhiteSpace(connstring)) > > throw new ApplicationException("Connection string > > cann't empty!"); > > > cfg.SetProperty(NHibernate.Cfg.Environment.CollectionTypeFactoryClass, > > typeof(uNhAddIns.WPF.Collections.WpfCollectionTypeFactory).AssemblyQualifie > > dName); > > > cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider, > > typeof(NHibernate.Connection.DriverConnectionProvider).AssemblyQualifiedNam > > e); > > > cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, > > connstring); > > cfg.SetProperty(NHibernate.Cfg.Environment.Dialect, > > typeof(NHibernate.Dialect.MsSql2008Dialect).AssemblyQualifiedName); > > > cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionDriver, > > typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName); > > cfg.SetProperty(NHibernate.Cfg.Environment.ShowSql, > > "true"); > > cfg.SetProperty(NHibernate.Cfg.Environment.DefaultSchema, > > "dbo"); > > > cfg.SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, > > typeof(NHibernate.ByteCode.Castle.ProxyFactoryFactory).AssemblyQualifiedNam > > e); > > > return cfg; > > } > > > private Configuration ReadMap() > > { > > cfg.AddAssembly("DomainAssembly"); > > > return cfg; > > } > > > public ISession GetSession(string connstring) > > { > > if (cfg == null) > > { > > cfg = new Configuration(); > > cfg = ReadConfig(connstring); > > } > > > if (sessionFactory == null) > > sessionFactory = cfg.BuildSessionFactory(); > > > return OpenSession(); > > } > > > private ISession OpenSession() > > { > > if (session == null || !session.IsOpen || ! > > session.IsConnected) > > session = sessionFactory.OpenSession(); > > > return session; > > } > > > public void Dispose() > > { > > if (session != null) > > session.Dispose(); > > } > > > public void CloseSession() > > { > > try > > { > > if (session != null) > > { > > session.Flush(); > > session.Close(); > > } > > } > > finally > > { > > session.Dispose(); > > } > > } > > > public void BeginTransaction(string connstring) > > { > > if (trans == null) > > { > > trans = GetSession(connstring).BeginTransaction(); > > } > > } > > > public void CommitTransaction() > > { > > try > > { > > if (trans != null && !trans.WasCommitted && ! > > trans.WasRolledBack) > > { > > trans.Commit(); > > } > > } > > catch (Exception ex) > > { > > RollbackTransaction(); > > throw ex; > > } > > } > > > public void RollbackTransaction() > > { > > try > > { > > if (trans != null && !trans.WasCommitted && ! > > trans.WasRolledBack) > > { > > trans.Rollback(); > > } > > } > > finally > > { > > CloseSession(); > > } > > } > > } > > =========================================================================== > > ============================== > > > ManagerBase.cs > > =========================================================================== > > ============================== > > public class ManagerBase<T> where T : DomainBase > > { > > public ReturnValueBoolean Refresh(T obj) > > { > > > SessionManager.Instance.BeginTransaction(RunTime.ConnString); > > ISession session = > > SessionManager.Instance.GetSession(RunTime.ConnString); > > > session.Refresh(obj); > > session.Flush(); > > SessionManager.Instance.CommitTransaction(); > > obj.ClearChanged(); > > > return new ReturnValueBoolean(0, true, "Success"); > > } > > > public ReturnValueBoolean Save(T obj) > > { > > > SessionManager.Instance.BeginTransaction(RunTime.ConnString); > > ISession session = > > SessionManager.Instance.GetSession(RunTime.ConnString); > > > session.Save(obj); > > session.Flush(); > > SessionManager.Instance.CommitTransaction(); > > obj.ClearChanged(); > > > return new ReturnValueBoolean(0, true, "Success"); > > } > > > public ReturnValueBoolean Update(T obj) > > { > > > SessionManager.Instance.BeginTransaction(RunTime.ConnString); > > ISession session = > > SessionManager.Instance.GetSession(RunTime.ConnString); > > > session.Update(obj); > > session.Flush(); > > SessionManager.Instance.CommitTransaction(); > > obj.ClearChanged(); > > > return new ReturnValueBoolean(0, true, "Success"); > > } > > > public ReturnValueBoolean SaveOrUpdate(T obj) > > { > > > SessionManager.Instance.BeginTransaction(RunTime.ConnString); > > ISession session = > > SessionManager.Instance.GetSession(RunTime.ConnString); > > > session.SaveOrUpdate(obj); > > session.Flush(); > > SessionManager.Instance.CommitTransaction(); > > obj.ClearChanged(); > > > return new ReturnValueBoolean(0, true, "Success"); > > } > > > public ReturnValueBoolean SaveOrUpdate(IList<T> lobj) > > { > > > SessionManager.Instance.BeginTransaction(RunTime.ConnString); > > ISession session = > > SessionManager.Instance.GetSession(RunTime.ConnString); > > > foreach (T obj in lobj) > > { > > session.SaveOrUpdate(obj); > > obj.ClearChanged(); > > } > > session.Flush(); > > SessionManager.Instance.CommitTransaction(); > > > return new ReturnValueBoolean(0, true, "Success"); > > } > > > public ReturnValueBoolean Delete(T obj) > > { > > > SessionManager.Instance.BeginTransaction(RunTime.ConnString); > > ISession session = > > SessionManager.Instance.GetSession(RunTime.ConnString); > > > session.Delete(obj); > > session.Flush(); > > SessionManager.Instance.CommitTransaction(); > > > return new ReturnValueBoolean(0, true, "Success"); > > } > > > public ReturnValue<T> Get(int id) > > { > > if (id <= 0) > > return new ReturnValue<T>(1, default(T), > > "failed"); > > else > > > > ... > > 阅读更多 >> -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
