public void Can_Use_Pessimistic_Concurrency() { CustomerWithGuid detachedCustomer; using (IUnitOfWork unitOfWork = new UnitOfWork()) { detachedCustomer = new CustomerWithGuid { Name = "Customer" }; unitOfWork.Session.Save(detachedCustomer); unitOfWork.Commit(); } Assert.Throws <GenericADOException>(() => { using (IUnitOfWork unitOfWork = new UnitOfWork()) { var customer = unitOfWork.Session.Get <CustomerWithGuid>(detachedCustomer.Id, LockMode.Upgrade); // with (updlock, rowlock) using (var statelessSession = _sessionFactory.OpenStatelessSession()) using (var statelessTransaction = statelessSession.BeginTransaction()) { var statelessCustomer = statelessSession.Get <CustomerWithGuid>(detachedCustomer.Id); statelessCustomer.Name = DateTime.Now.ToString(); statelessSession.Update(statelessCustomer); statelessTransaction.Commit(); } customer.Name = DateTime.Now.ToString(); unitOfWork.Commit(); } }); }
public void Can_Use_Future_Queries() { // http://ayende.com/blog/3979/nhibernate-futures using (IUnitOfWork unitOfWork = new UnitOfWork()) { for (int i = 0; i < 10; i++) { var post = new CustomerWithGuid { Name = string.Format("Customer: {0}", i) }; unitOfWork.Session.Save(post); } unitOfWork.Commit(); } // Without futures using (IUnitOfWork unitOfWork = new UnitOfWork()) { var customers = unitOfWork.Session.QueryOver <CustomerWithGuid>() .Take(5) .List(); var customerCount = unitOfWork.Session.QueryOver <CustomerWithGuid>() .RowCount(); unitOfWork.Commit(); } // With futures using (IUnitOfWork unitOfWork = new UnitOfWork()) { IEnumerable <CustomerWithGuid> customers = unitOfWork.Session.QueryOver <CustomerWithGuid>() .Take(5) .Future <CustomerWithGuid>(); IFutureValue <int> customerCount = unitOfWork.Session.QueryOver <CustomerWithGuid>() .Select(Projections.RowCount()) .FutureValue <int>(); var count = customerCount.Value; unitOfWork.Commit(); } Assert.Equal(10, _sessionFactory.Statistics.EntityInsertCount); }
public void Can_Batch_Inserts_With_Guid() { using (var session = _sessionFactory.OpenSession()) using (var transaction = session.BeginTransaction(IsolationLevel.ReadUncommitted)) { for (int i = 0; i < 20; i++) { var customer = new CustomerWithGuid { Name = string.Format("Customer: {0}", i) }; session.Save(customer); } transaction.Commit(); } Assert.Equal(20, _sessionFactory.Statistics.EntityInsertCount); }
public void Can_Use_Unit_Of_Work_Instead() { // http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx using (IUnitOfWork unitOfWork = new UnitOfWork()) { for (int i = 0; i < 20; i++) { var customer = new CustomerWithGuid { Name = string.Format("Customer: {0}", i) }; unitOfWork.Session.Save(customer); } unitOfWork.Commit(); } Assert.Equal(20, _sessionFactory.Statistics.EntityInsertCount); }
public void Can_Use_Pessimistic_Concurrency() { CustomerWithGuid detachedCustomer; using (IUnitOfWork unitOfWork = new UnitOfWork()) { detachedCustomer = new CustomerWithGuid { Name = "Customer" }; unitOfWork.Session.Save(detachedCustomer); unitOfWork.Commit(); } Assert.Throws<GenericADOException>(() => { using (IUnitOfWork unitOfWork = new UnitOfWork()) { var customer = unitOfWork.Session.Get<CustomerWithGuid>(detachedCustomer.Id, LockMode.Upgrade); // with (updlock, rowlock) using (var statelessSession = _sessionFactory.OpenStatelessSession()) using (var statelessTransaction = statelessSession.BeginTransaction()) { var statelessCustomer = statelessSession.Get<CustomerWithGuid>(detachedCustomer.Id); statelessCustomer.Name = DateTime.Now.ToString(); statelessSession.Update(statelessCustomer); statelessTransaction.Commit(); } customer.Name = DateTime.Now.ToString(); unitOfWork.Commit(); } }); }
public void Can_Use_Future_Queries() { // http://ayende.com/blog/3979/nhibernate-futures using (IUnitOfWork unitOfWork = new UnitOfWork()) { for (int i = 0; i < 10; i++) { var post = new CustomerWithGuid { Name = string.Format("Customer: {0}", i) }; unitOfWork.Session.Save(post); } unitOfWork.Commit(); } // Without futures using (IUnitOfWork unitOfWork = new UnitOfWork()) { var customers = unitOfWork.Session.QueryOver<CustomerWithGuid>() .Take(5) .List(); var customerCount = unitOfWork.Session.QueryOver<CustomerWithGuid>() .RowCount(); unitOfWork.Commit(); } // With futures using (IUnitOfWork unitOfWork = new UnitOfWork()) { IEnumerable<CustomerWithGuid> customers = unitOfWork.Session.QueryOver<CustomerWithGuid>() .Take(5) .Future<CustomerWithGuid>(); IFutureValue<int> customerCount = unitOfWork.Session.QueryOver<CustomerWithGuid>() .Select(Projections.RowCount()) .FutureValue<int>(); var count = customerCount.Value; unitOfWork.Commit(); } Assert.Equal(10, _sessionFactory.Statistics.EntityInsertCount); }