NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); cfg.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider"); cfg.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver"); cfg.SetProperty("hibernate.connection.connection_string", "Server=myserver;initial catalog=ABC;User Id=sa;Password="); cfg.SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate"); cfg.SetProperty("hibernate.use_outer_join", "true"); cfg.SetProperty("hibernate.show_sql", "false"); NHibernate.ISessionFactory sessionFactory = cfg.BuildSessionFactory(); using (NHibernate.ISession sess1 = sessionFactory.OpenSession()) { ... } NHibernate.ISession sess2 = sessionFactory.OpenSession(); try { ... } catch (Exception ex) { } finally { sess2.Close(); }
關於 Session Close
A call to ISession.Close() marks the end of a session. The main implication of Close() is that the ADO.NET connection will be relinquished by the session.
關於 Session Flush
Flushing synchronizes the persistent store with in-memory changes but not vice-versa. Note that for all NHibernate ADO.NET connections/transactions, the transaction isolation level for that connection applies to all operations executed by NHibernate.
From time to time the ISession synchronizes its persistent state with the database. Performance will be affected if this process occurs too often. You may sometimes minimize unnecessary flushing by disabling automatic flushing or even by changing the order of queries and other operations within a particular transaction.
sess.Save(cat); sess.Flush(); //force the SQL INSERT sess.Refresh(cat); //re-read the state (after the trigger executes)
DomesticCat cat = (DomesticCat) sess.Load( typeof(Cat), 69L ); cat.Name = "PK" sess.Flush(); // changes to cat are automatically detected and persisted
參考資料:http://www.hibernate.org/
0 comments:
Post a Comment