diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH3848/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH3848/Fixture.cs index 26ecc4db471..a7bbd5e8e8c 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/NH3848/Fixture.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH3848/Fixture.cs @@ -161,390 +161,498 @@ protected override void OnTearDown() [Test] public virtual async Task ChildCollectionsFromLeftOuterJoinWithOnClauseRestrictionOnCollectionShouldNotBeInSecondLevelCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = await (GetCustomersByOrderNumberUsingOnClauseAsync(firstSession, OrderNumber)); - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer3.Id).Orders, - Has.Count.EqualTo(Customer3.Orders.Count(n => n.Number == OrderNumber))); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - - firstSession.Dispose(); - secondSession.Dispose(); + using (var firstSession = OpenSession()) + { + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + await (GetCustomersByOrderNumberUsingOnClauseAsync(firstSession, OrderNumber)); + await (tx.CommitAsync()); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count(n => n.Number == OrderNumber))); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + + await (tx.CommitAsync()); + } + } } [Test] public virtual async Task ChildCollectionsWithSelectModeFetchOnCollectionShouldNotBeInSecondLevelCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = await (GetCustomersByOrderNumberUsingFetchAsync(firstSession, OrderNumber)); - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2, - Has.Count.EqualTo(2)); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - - firstSession.Dispose(); - secondSession.Dispose(); + using (var firstSession = OpenSession()) + { + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = await (GetCustomersByOrderNumberUsingFetchAsync(firstSession, OrderNumber)); + await (tx.CommitAsync()); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2, + Has.Count.EqualTo(2)); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + + await (tx.CommitAsync()); + } + } } [Test] public virtual async Task ChildCollectionsWithSelectModeFetchAndWhereClauseShouldNotBeInSecondLevelCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = await (GetCustomersByOrderNumberUsingFetchAndWhereClauseAsync(firstSession, OrderNumber)); - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2, - Has.Count.EqualTo(2)); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - - firstSession.Dispose(); - secondSession.Dispose(); + using (var firstSession = OpenSession()) + { + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + await (GetCustomersByOrderNumberUsingFetchAndWhereClauseAsync(firstSession, OrderNumber)); + await (tx.CommitAsync()); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2, + Has.Count.EqualTo(2)); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + + await (tx.CommitAsync()); + } + } } [Test] public async Task ChildCollectionsFromLeftOuterJoinWithWhereClauseRestrictionOnCollectionShouldNotBeInSecondLevelCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = await (GetCustomersByOrderNumberUsingWhereClauseAsync(firstSession, OrderNumber)); - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); + using (var firstSession = OpenSession()) + { + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + await (GetCustomersByOrderNumberUsingWhereClauseAsync(firstSession, OrderNumber)); + await (tx.CommitAsync()); + } - firstSession.Dispose(); - secondSession.Dispose(); + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + + await (tx.CommitAsync()); + } + } } [Test] public async Task ChildCollectionsEagerFetchedShouldBeInSecondLevelCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = await (GetCustomersWithOrdersEagerLoadedAsync(firstSession)); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count)); + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = await (GetCustomersWithOrdersEagerLoadedAsync(firstSession)); + await (tx.CommitAsync()); + } - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + } - firstSession.Dispose(); - secondSession.Dispose(); + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + + await (tx.CommitAsync()); + } + } } [Test] public async Task ChildCollectionsFromLeftOuterJoinWithWhereClauseRestrictionOnRootShouldBeInSecondLevelCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = await (GetCustomersByNameUsingWhereClauseAsync(firstSession, "First Customer")); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + await (GetCustomersByNameUsingWhereClauseAsync(firstSession, "First Customer")); + await (tx.CommitAsync()); + } + + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + } - var secondSession = OpenSession(); - var customers = await (secondSession.GetAsync(Customer1.Id)); + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (secondSession.GetAsync(Customer1.Id)); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Orders, Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That(customers.Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - firstSession.Dispose(); - secondSession.Dispose(); + await (tx.CommitAsync()); + } + } } [Test] public async Task ChildCollectionsFromLeftOuterJoinShouldBeInSecondLevelCacheIfQueryContainsSubqueryWithRestrictionOnLeftOuterJoinAsync() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = - await (GetCustomersByOrderNumberUsingSubqueriesAndByNameUsingWhereClauseAsync( - firstSession, - OrderNumber, - Customer1.Name)); - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count)); - - using (var thirdSession = OpenSession()) - using (IDbCommand cmd = thirdSession.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + await (GetCustomersByOrderNumberUsingSubqueriesAndByNameUsingWhereClauseAsync( + firstSession, + OrderNumber, + Customer1.Name)); + await (tx.CommitAsync()); + } + + using (var secondSession = OpenSession()) + { + IList customers; + using (var tx = secondSession.BeginTransaction()) + { + customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); + await (tx.CommitAsync()); + } - firstSession.Dispose(); - secondSession.Dispose(); + using (var thirdSession = OpenSession()) + using (IDbCommand cmd = thirdSession.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + } + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); + } + } } [Test] public virtual async Task ChildCollectionsFromLeftOuterJoinOnlyWithRestrictionShouldNotBeIn2LvlCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2AndCompanies = - await (GetCustomersWithCompaniesByOrderNumberUsingOnClauseAsync(firstSession, OrderNumber)); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } + IList customersWithOrderNumberEqualsTo2AndCompanies; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2AndCompanies = + await (GetCustomersWithCompaniesByOrderNumberUsingOnClauseAsync(firstSession, OrderNumber)); + await (tx.CommitAsync()); + } - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) - { - cmd.CommandText = "DELETE FROM Companies"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "DELETE FROM Companies"; + cmd.ExecuteNonQuery(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count(n => n.Number == OrderNumber))); + + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count)); + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count)); + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(Customer3.Companies.Count)); + + Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(Customer3.Companies.Count)); + + await (tx.CommitAsync()); + } } - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer3.Id).Orders, - Has.Count.EqualTo(Customer3.Orders.Count(n => n.Number == OrderNumber))); - - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count)); - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count)); - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(Customer3.Companies.Count)); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); - - Assert.That( - customers.Single(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count)); - Assert.That( - customers.Single(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count)); - Assert.That( - customers.Single(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(Customer3.Companies.Count)); - - firstSession.Dispose(); - secondSession.Dispose(); } [Test] public virtual async Task ChildCollectionsWithoutRestrictionShouldBeIn2LvlCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrdersAndCompaniesWithoutRestrictions = - await (GetCustomersAndCompaniesByOrderNumberUsingFetchWithoutRestrictionsAsync(firstSession)); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } + IList customersWithOrdersAndCompaniesWithoutRestrictions; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrdersAndCompaniesWithoutRestrictions = + await (GetCustomersAndCompaniesByOrderNumberUsingFetchWithoutRestrictionsAsync(firstSession)); + await (tx.CommitAsync()); + } - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) - { - cmd.CommandText = "DELETE FROM Companies"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "DELETE FROM Companies"; + cmd.ExecuteNonQuery(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count())); + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count())); + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count())); + + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count)); + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count)); + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(Customer3.Companies.Count)); + + Assert.That( + customers.First(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count())); + Assert.That( + customers.First(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count())); + Assert.That( + customers.First(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count())); + + Assert.That( + customers.First(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count())); + Assert.That( + customers.First(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count())); + Assert.That( + customers.First(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(Customer3.Companies.Count())); + + await (tx.CommitAsync()); + } } - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count())); - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count())); - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer3.Id).Orders, - Has.Count.EqualTo(Customer3.Orders.Count())); - - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count)); - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count)); - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(Customer3.Companies.Count)); - - Assert.That( - customers.First(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count())); - Assert.That( - customers.First(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count())); - Assert.That( - customers.First(n => n.Id == Customer3.Id).Orders, - Has.Count.EqualTo(Customer3.Orders.Count())); - - Assert.That( - customers.First(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count())); - Assert.That( - customers.First(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count())); - Assert.That( - customers.First(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(Customer3.Companies.Count())); - - firstSession.Dispose(); - secondSession.Dispose(); } [Test] public virtual async Task ChildCollectionsWithRestrictionShouldNotBeIn2LvlCacheAsync() { - var firstSession = OpenSession(); - var customersWithOrdersAndCompaniesWithRestrictions = - await (GetCustomersAndCompaniesByOrderNumberUsingFetchAndWhereClauseAsync(firstSession, OrderNumber, "Second")); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) - { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Companies"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); + IList customersWithOrdersAndCompaniesWithRestrictions; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrdersAndCompaniesWithRestrictions = + await (GetCustomersAndCompaniesByOrderNumberUsingFetchAndWhereClauseAsync( + firstSession, + OrderNumber, + "Second")); + await (tx.CommitAsync()); + } + + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "DELETE FROM Companies"; + cmd.ExecuteNonQuery(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = await (GetAllCustomersAsync(secondSession)); + + Assert.That( + customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + + Assert.That( + customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count(n => n.Name == "Second"))); + Assert.That( + customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count(n => n.Name == "Second"))); + + Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(0)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(0)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(0)); + + await (tx.CommitAsync()); + } } - - var secondSession = OpenSession(); - var customers = await (GetAllCustomersAsync(secondSession)); - - Assert.That( - customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - - Assert.That( - customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count(n => n.Name == "Second"))); - Assert.That( - customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count(n => n.Name == "Second"))); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); - - Assert.That( - customers.Single(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(0)); - Assert.That( - customers.Single(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(0)); - Assert.That( - customers.Single(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(0)); - - firstSession.Dispose(); - secondSession.Dispose(); } protected async Task ClearSecondLevelCacheForAsync(System.Type entity, CancellationToken cancellationToken = default(CancellationToken)) diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH3848/HqlTestFixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH3848/HqlTestFixture.cs new file mode 100644 index 00000000000..6f7588dc11a --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH3848/HqlTestFixture.cs @@ -0,0 +1,184 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using System; +using System.Collections.Generic; +using NHibernate.Transform; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH3848 +{ + using System.Threading.Tasks; + using System.Threading; + public class HqlTestFixtureAsync : FixtureAsync + { + [Ignore("We can't write such query using hql, because we can't use 'with' clause when we're fetching collection.")] + public override Task ChildCollectionsFromLeftOuterJoinWithOnClauseRestrictionOnCollectionShouldNotBeInSecondLevelCacheAsync() + { + throw new NotImplementedException(); + } + + [Ignore("We can't write such query using hql, because we can't use 'with' clause when we're fetching collection.")] + public override Task ChildCollectionsFromLeftOuterJoinOnlyWithRestrictionShouldNotBeIn2LvlCacheAsync() + { + throw new NotImplementedException(); + } + + [Ignore("We can't write such query using hql, because we can't use 'with' clause when we're fetching collection.")] + public override Task ChildCollectionsWithRestrictionShouldNotBeIn2LvlCacheAsync() + { + throw new NotImplementedException(); + } + + [Ignore("We can't write such query using hql, because we can't use 'with' clause when we're fetching collection.")] + public override Task ChildCollectionsWithSelectModeFetchOnCollectionShouldNotBeInSecondLevelCacheAsync() + { + throw new NotImplementedException(); + } + + protected override Task> GetCustomersWithOrdersEagerLoadedAsync(ISession session, CancellationToken cancellationToken = default(CancellationToken)) + { + try + { + var query = session.CreateQuery("from Customer as c left join fetch c.Orders as o"); + query.SetResultTransformer(new DistinctRootEntityResultTransformer()); + return query.ListAsync(cancellationToken); + } + catch (Exception ex) + { + return Task.FromException>(ex); + } + } + + //We can't write such query using hql, because we can't use 'with' clause when we're fetching collection. + protected override Task> GetCustomersByOrderNumberUsingOnClauseAsync(ISession session, int orderNumber, CancellationToken cancellationToken = default(CancellationToken)) + { + throw new NotImplementedException(); + } + + protected override Task> GetCustomersByOrderNumberUsingWhereClauseAsync(ISession session, int orderNumber, CancellationToken cancellationToken = default(CancellationToken)) + { + try + { + var query = session.CreateQuery( + "from Customer as c " + + "left join fetch c.Orders as o " + + "where o.Number = :number"); + query.SetParameter("number", orderNumber); + return query.ListAsync(cancellationToken); + } + catch (Exception ex) + { + return Task.FromException>(ex); + } + } + + protected override Task> GetCustomersByNameUsingWhereClauseAsync(ISession session, string customerName, CancellationToken cancellationToken = default(CancellationToken)) + { + try + { + var query = session.CreateQuery( + "from Customer as c " + + "left join fetch c.Orders as o " + + "where c.Name = :name"); + query.SetParameter("name", customerName); + query.SetResultTransformer(new DistinctRootEntityResultTransformer()); + return query.ListAsync(cancellationToken); + } + catch (Exception ex) + { + return Task.FromException>(ex); + } + } + + //We can't write such query using hql, because we can't use 'with' clause when we're fetching collection. + protected override Task> GetCustomersByOrderNumberUsingFetchAsync(ISession session, int orderNumber, CancellationToken cancellationToken = default(CancellationToken)) + { + throw new NotImplementedException(); + } + + protected override Task> GetCustomersByOrderNumberUsingFetchAndWhereClauseAsync(ISession session, int orderNumber, CancellationToken cancellationToken = default(CancellationToken)) + { + try + { + var query = session.CreateQuery( + "from Customer as c " + + "inner join fetch c.Orders as o " + + "where o.Number = :number"); + query.SetParameter("number", orderNumber); + return query.ListAsync(cancellationToken); + } + catch (Exception ex) + { + return Task.FromException>(ex); + } + } + + //We can't write such query using hql, because we can't use 'with' clause when we're fetching collection. + protected override Task> GetCustomersAndCompaniesByOrderNumberUsingFetchAndWhereClauseAsync(ISession session, int orderNumber, string name, CancellationToken cancellationToken = default(CancellationToken)) + { + throw new NotImplementedException(); + } + + protected override Task> GetCustomersAndCompaniesByOrderNumberUsingFetchWithoutRestrictionsAsync(ISession session, CancellationToken cancellationToken = default(CancellationToken)) + { + try + { + var query = session.CreateQuery( + "from Customer as c " + + "inner join fetch c.Orders as o " + + "left join fetch c.Companies as cmp"); + return query.ListAsync(cancellationToken); + } + catch (Exception ex) + { + return Task.FromException>(ex); + } + } + + protected override Task> GetCustomersByOrderNumberUsingSubqueriesAndByNameUsingWhereClauseAsync(ISession session, int orderNumber, string customerName, CancellationToken cancellationToken = default(CancellationToken)) + { + try + { + var query = session.CreateQuery( + "from Customer as c " + + "left join fetch c.Orders as o " + + "where c.Name = :name and " + + "c.Id in (select c1.Id from Customer as c1 left join c1.Orders as o2 where o2.Number = :number)"); + query.SetParameter("name", customerName); + query.SetParameter("number", orderNumber); + query.SetResultTransformer(new DistinctRootEntityResultTransformer()); + return query.ListAsync(cancellationToken); + } + catch (Exception ex) + { + return Task.FromException>(ex); + } + } + + //We can't write such query using hql, because we can't use 'with' clause when we're fetching collection. + protected override Task> GetCustomersWithCompaniesByOrderNumberUsingOnClauseAsync(ISession session, int orderNumber, CancellationToken cancellationToken = default(CancellationToken)) + { + throw new NotImplementedException(); + } + + protected override Task> GetAllCustomersAsync(ISession session, CancellationToken cancellationToken = default(CancellationToken)) + { + try + { + return session.CreateQuery("from Customer as c").ListAsync(cancellationToken); + } + catch (Exception ex) + { + return Task.FromException>(ex); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/NH3848/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH3848/Fixture.cs index 84fca8232d3..c4514867255 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH3848/Fixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH3848/Fixture.cs @@ -149,390 +149,498 @@ protected override void OnTearDown() [Test] public virtual void ChildCollectionsFromLeftOuterJoinWithOnClauseRestrictionOnCollectionShouldNotBeInSecondLevelCache() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = GetCustomersByOrderNumberUsingOnClause(firstSession, OrderNumber); - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer3.Id).Orders, - Has.Count.EqualTo(Customer3.Orders.Count(n => n.Number == OrderNumber))); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - - firstSession.Dispose(); - secondSession.Dispose(); + using (var firstSession = OpenSession()) + { + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + GetCustomersByOrderNumberUsingOnClause(firstSession, OrderNumber); + tx.Commit(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count(n => n.Number == OrderNumber))); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + + tx.Commit(); + } + } } [Test] public virtual void ChildCollectionsWithSelectModeFetchOnCollectionShouldNotBeInSecondLevelCache() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = GetCustomersByOrderNumberUsingFetch(firstSession, OrderNumber); - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2, - Has.Count.EqualTo(2)); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - - firstSession.Dispose(); - secondSession.Dispose(); + using (var firstSession = OpenSession()) + { + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = GetCustomersByOrderNumberUsingFetch(firstSession, OrderNumber); + tx.Commit(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2, + Has.Count.EqualTo(2)); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + + tx.Commit(); + } + } } [Test] public virtual void ChildCollectionsWithSelectModeFetchAndWhereClauseShouldNotBeInSecondLevelCache() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = GetCustomersByOrderNumberUsingFetchAndWhereClause(firstSession, OrderNumber); - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2, - Has.Count.EqualTo(2)); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - - firstSession.Dispose(); - secondSession.Dispose(); + using (var firstSession = OpenSession()) + { + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + GetCustomersByOrderNumberUsingFetchAndWhereClause(firstSession, OrderNumber); + tx.Commit(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2, + Has.Count.EqualTo(2)); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + + tx.Commit(); + } + } } [Test] public void ChildCollectionsFromLeftOuterJoinWithWhereClauseRestrictionOnCollectionShouldNotBeInSecondLevelCache() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = GetCustomersByOrderNumberUsingWhereClause(firstSession, OrderNumber); - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); + using (var firstSession = OpenSession()) + { + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + GetCustomersByOrderNumberUsingWhereClause(firstSession, OrderNumber); + tx.Commit(); + } - firstSession.Dispose(); - secondSession.Dispose(); + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + + tx.Commit(); + } + } } [Test] public void ChildCollectionsEagerFetchedShouldBeInSecondLevelCache() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = GetCustomersWithOrdersEagerLoaded(firstSession); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count)); + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = GetCustomersWithOrdersEagerLoaded(firstSession); + tx.Commit(); + } - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(Customer3.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(Customer2.Orders.Count)); + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + } - firstSession.Dispose(); - secondSession.Dispose(); + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count)); + + tx.Commit(); + } + } } [Test] public void ChildCollectionsFromLeftOuterJoinWithWhereClauseRestrictionOnRootShouldBeInSecondLevelCache() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = GetCustomersByNameUsingWhereClause(firstSession, "First Customer"); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + GetCustomersByNameUsingWhereClause(firstSession, "First Customer"); + tx.Commit(); + } + + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + } - var secondSession = OpenSession(); - var customers = secondSession.Get(Customer1.Id); + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = secondSession.Get(Customer1.Id); - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Orders, Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That(customers.Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - firstSession.Dispose(); - secondSession.Dispose(); + tx.Commit(); + } + } } [Test] public void ChildCollectionsFromLeftOuterJoinShouldBeInSecondLevelCacheIfQueryContainsSubqueryWithRestrictionOnLeftOuterJoin() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2 = - GetCustomersByOrderNumberUsingSubqueriesAndByNameUsingWhereClause( - firstSession, - OrderNumber, - Customer1.Name); - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count)); - - using (var thirdSession = OpenSession()) - using (IDbCommand cmd = thirdSession.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } + IList customersWithOrderNumberEqualsTo2; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2 = + GetCustomersByOrderNumberUsingSubqueriesAndByNameUsingWhereClause( + firstSession, + OrderNumber, + Customer1.Name); + tx.Commit(); + } + + using (var secondSession = OpenSession()) + { + IList customers; + using (var tx = secondSession.BeginTransaction()) + { + customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrderNumberEqualsTo2.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(Customer1.Orders.Count)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); + tx.Commit(); + } - firstSession.Dispose(); - secondSession.Dispose(); + using (var thirdSession = OpenSession()) + using (IDbCommand cmd = thirdSession.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + } + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count)); + Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); + } + } } [Test] public virtual void ChildCollectionsFromLeftOuterJoinOnlyWithRestrictionShouldNotBeIn2LvlCache() { - var firstSession = OpenSession(); - var customersWithOrderNumberEqualsTo2AndCompanies = - GetCustomersWithCompaniesByOrderNumberUsingOnClause(firstSession, OrderNumber); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } + IList customersWithOrderNumberEqualsTo2AndCompanies; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrderNumberEqualsTo2AndCompanies = + GetCustomersWithCompaniesByOrderNumberUsingOnClause(firstSession, OrderNumber); + tx.Commit(); + } - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) - { - cmd.CommandText = "DELETE FROM Companies"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "DELETE FROM Companies"; + cmd.ExecuteNonQuery(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count(n => n.Number == OrderNumber))); + + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count)); + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count)); + Assert.That( + customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(Customer3.Companies.Count)); + + Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(Customer3.Companies.Count)); + + tx.Commit(); + } } - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer3.Id).Orders, - Has.Count.EqualTo(Customer3.Orders.Count(n => n.Number == OrderNumber))); - - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count)); - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count)); - Assert.That( - customersWithOrderNumberEqualsTo2AndCompanies.First(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(Customer3.Companies.Count)); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); - - Assert.That( - customers.Single(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count)); - Assert.That( - customers.Single(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count)); - Assert.That( - customers.Single(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(Customer3.Companies.Count)); - - firstSession.Dispose(); - secondSession.Dispose(); } [Test] public virtual void ChildCollectionsWithoutRestrictionShouldBeIn2LvlCache() { - var firstSession = OpenSession(); - var customersWithOrdersAndCompaniesWithoutRestrictions = - GetCustomersAndCompaniesByOrderNumberUsingFetchWithoutRestrictions(firstSession); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } + IList customersWithOrdersAndCompaniesWithoutRestrictions; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrdersAndCompaniesWithoutRestrictions = + GetCustomersAndCompaniesByOrderNumberUsingFetchWithoutRestrictions(firstSession); + tx.Commit(); + } - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) - { - cmd.CommandText = "DELETE FROM Companies"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "DELETE FROM Companies"; + cmd.ExecuteNonQuery(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count())); + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count())); + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count())); + + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count)); + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count)); + Assert.That( + customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(Customer3.Companies.Count)); + + Assert.That( + customers.First(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count())); + Assert.That( + customers.First(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count())); + Assert.That( + customers.First(n => n.Id == Customer3.Id).Orders, + Has.Count.EqualTo(Customer3.Orders.Count())); + + Assert.That( + customers.First(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count())); + Assert.That( + customers.First(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count())); + Assert.That( + customers.First(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(Customer3.Companies.Count())); + + tx.Commit(); + } } - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count())); - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count())); - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer3.Id).Orders, - Has.Count.EqualTo(Customer3.Orders.Count())); - - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count)); - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count)); - Assert.That( - customersWithOrdersAndCompaniesWithoutRestrictions.First(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(Customer3.Companies.Count)); - - Assert.That( - customers.First(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count())); - Assert.That( - customers.First(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count())); - Assert.That( - customers.First(n => n.Id == Customer3.Id).Orders, - Has.Count.EqualTo(Customer3.Orders.Count())); - - Assert.That( - customers.First(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count())); - Assert.That( - customers.First(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count())); - Assert.That( - customers.First(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(Customer3.Companies.Count())); - - firstSession.Dispose(); - secondSession.Dispose(); } [Test] public virtual void ChildCollectionsWithRestrictionShouldNotBeIn2LvlCache() { - var firstSession = OpenSession(); - var customersWithOrdersAndCompaniesWithRestrictions = - GetCustomersAndCompaniesByOrderNumberUsingFetchAndWhereClause(firstSession, OrderNumber, "Second"); - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) - { - cmd.CommandText = "DELETE FROM Orders"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); - } - - using (var session = OpenSession()) - using (IDbCommand cmd = session.Connection.CreateCommand()) + using (var firstSession = OpenSession()) { - cmd.CommandText = "DELETE FROM Companies"; - cmd.ExecuteNonQuery(); - cmd.Connection.Close(); + IList customersWithOrdersAndCompaniesWithRestrictions; + using (var tx = firstSession.BeginTransaction()) + { + customersWithOrdersAndCompaniesWithRestrictions = + GetCustomersAndCompaniesByOrderNumberUsingFetchAndWhereClause( + firstSession, + OrderNumber, + "Second"); + tx.Commit(); + } + + using (var session = OpenSession()) + using (IDbCommand cmd = session.Connection.CreateCommand()) + { + cmd.CommandText = "DELETE FROM Orders"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "DELETE FROM Companies"; + cmd.ExecuteNonQuery(); + } + + using (var secondSession = OpenSession()) + using (var tx = secondSession.BeginTransaction()) + { + var customers = GetAllCustomers(secondSession); + + Assert.That( + customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer1.Id).Orders, + Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); + Assert.That( + customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer2.Id).Orders, + Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); + + Assert.That( + customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(Customer1.Companies.Count(n => n.Name == "Second"))); + Assert.That( + customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(Customer2.Companies.Count(n => n.Name == "Second"))); + + Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); + Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); + + Assert.That( + customers.Single(n => n.Id == Customer1.Id).Companies, + Has.Count.EqualTo(0)); + Assert.That( + customers.Single(n => n.Id == Customer2.Id).Companies, + Has.Count.EqualTo(0)); + Assert.That( + customers.Single(n => n.Id == Customer3.Id).Companies, + Has.Count.EqualTo(0)); + + tx.Commit(); + } } - - var secondSession = OpenSession(); - var customers = GetAllCustomers(secondSession); - - Assert.That( - customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer1.Id).Orders, - Has.Count.EqualTo(Customer1.Orders.Count(n => n.Number == OrderNumber))); - Assert.That( - customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer2.Id).Orders, - Has.Count.EqualTo(Customer2.Orders.Count(n => n.Number == OrderNumber))); - - Assert.That( - customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(Customer1.Companies.Count(n => n.Name == "Second"))); - Assert.That( - customersWithOrdersAndCompaniesWithRestrictions.First(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(Customer2.Companies.Count(n => n.Name == "Second"))); - - Assert.That(customers.Single(n => n.Id == Customer1.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer2.Id).Orders, Has.Count.EqualTo(0)); - Assert.That(customers.Single(n => n.Id == Customer3.Id).Orders, Has.Count.EqualTo(0)); - - Assert.That( - customers.Single(n => n.Id == Customer1.Id).Companies, - Has.Count.EqualTo(0)); - Assert.That( - customers.Single(n => n.Id == Customer2.Id).Companies, - Has.Count.EqualTo(0)); - Assert.That( - customers.Single(n => n.Id == Customer3.Id).Companies, - Has.Count.EqualTo(0)); - - firstSession.Dispose(); - secondSession.Dispose(); } protected void ClearSecondLevelCacheFor(System.Type entity) diff --git a/src/NHibernate.Test/NHSpecificTest/NH3848/HqlTestFixture.cs b/src/NHibernate.Test/NHSpecificTest/NH3848/HqlTestFixture.cs new file mode 100644 index 00000000000..afd7b14dff3 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH3848/HqlTestFixture.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using NHibernate.Transform; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH3848 +{ + public class HqlTestFixture : Fixture + { + [Ignore("We can't write such query using hql, because we can't use 'with' clause when we're fetching collection.")] + public override void ChildCollectionsFromLeftOuterJoinWithOnClauseRestrictionOnCollectionShouldNotBeInSecondLevelCache() + { + throw new NotImplementedException(); + } + + [Ignore("We can't write such query using hql, because we can't use 'with' clause when we're fetching collection.")] + public override void ChildCollectionsFromLeftOuterJoinOnlyWithRestrictionShouldNotBeIn2LvlCache() + { + throw new NotImplementedException(); + } + + [Ignore("We can't write such query using hql, because we can't use 'with' clause when we're fetching collection.")] + public override void ChildCollectionsWithRestrictionShouldNotBeIn2LvlCache() + { + throw new NotImplementedException(); + } + + [Ignore("We can't write such query using hql, because we can't use 'with' clause when we're fetching collection.")] + public override void ChildCollectionsWithSelectModeFetchOnCollectionShouldNotBeInSecondLevelCache() + { + throw new NotImplementedException(); + } + + protected override IList GetCustomersWithOrdersEagerLoaded(ISession session) + { + var query = session.CreateQuery("from Customer as c left join fetch c.Orders as o"); + query.SetResultTransformer(new DistinctRootEntityResultTransformer()); + return query.List(); + } + + //We can't write such query using hql, because we can't use 'with' clause when we're fetching collection. + protected override IList GetCustomersByOrderNumberUsingOnClause(ISession session, int orderNumber) + { + throw new NotImplementedException(); + } + + protected override IList GetCustomersByOrderNumberUsingWhereClause(ISession session, int orderNumber) + { + var query = session.CreateQuery( + "from Customer as c " + + "left join fetch c.Orders as o " + + "where o.Number = :number"); + query.SetParameter("number", orderNumber); + return query.List(); + } + + protected override IList GetCustomersByNameUsingWhereClause(ISession session, string customerName) + { + var query = session.CreateQuery( + "from Customer as c " + + "left join fetch c.Orders as o " + + "where c.Name = :name"); + query.SetParameter("name", customerName); + query.SetResultTransformer(new DistinctRootEntityResultTransformer()); + return query.List(); + } + + //We can't write such query using hql, because we can't use 'with' clause when we're fetching collection. + protected override IList GetCustomersByOrderNumberUsingFetch(ISession session, int orderNumber) + { + throw new NotImplementedException(); + } + + protected override IList GetCustomersByOrderNumberUsingFetchAndWhereClause(ISession session, int orderNumber) + { + var query = session.CreateQuery( + "from Customer as c " + + "inner join fetch c.Orders as o " + + "where o.Number = :number"); + query.SetParameter("number", orderNumber); + return query.List(); + } + + //We can't write such query using hql, because we can't use 'with' clause when we're fetching collection. + protected override IList GetCustomersAndCompaniesByOrderNumberUsingFetchAndWhereClause(ISession session, int orderNumber, string name) + { + throw new NotImplementedException(); + } + + protected override IList GetCustomersAndCompaniesByOrderNumberUsingFetchWithoutRestrictions(ISession session) + { + var query = session.CreateQuery( + "from Customer as c " + + "inner join fetch c.Orders as o " + + "left join fetch c.Companies as cmp"); + return query.List(); + } + + protected override IList GetCustomersByOrderNumberUsingSubqueriesAndByNameUsingWhereClause(ISession session, int orderNumber, string customerName) + { + var query = session.CreateQuery( + "from Customer as c " + + "left join fetch c.Orders as o " + + "where c.Name = :name and " + + "c.Id in (select c1.Id from Customer as c1 left join c1.Orders as o2 where o2.Number = :number)"); + query.SetParameter("name", customerName); + query.SetParameter("number", orderNumber); + query.SetResultTransformer(new DistinctRootEntityResultTransformer()); + return query.List(); + } + + //We can't write such query using hql, because we can't use 'with' clause when we're fetching collection. + protected override IList GetCustomersWithCompaniesByOrderNumberUsingOnClause(ISession session, int orderNumber) + { + throw new NotImplementedException(); + } + + protected override IList GetAllCustomers(ISession session) + { + return session.CreateQuery("from Customer as c").List(); + } + } +} diff --git a/src/NHibernate/Async/Cache/UpdateTimestampsCache.cs b/src/NHibernate/Async/Cache/UpdateTimestampsCache.cs index 0b778f5edac..e5ba6ce0e45 100644 --- a/src/NHibernate/Async/Cache/UpdateTimestampsCache.cs +++ b/src/NHibernate/Async/Cache/UpdateTimestampsCache.cs @@ -72,7 +72,7 @@ public virtual async Task PreInvalidateAsync(IReadOnlyCollection spaces, } //Since v5.1 - [Obsolete("Please use PreInvalidate(IReadOnlyCollection) instead.")] + [Obsolete("Please use Invalidate(IReadOnlyCollection) instead.")] public Task InvalidateAsync(object[] spaces, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) diff --git a/src/NHibernate/Async/Hql/Ast/ANTLR/QueryTranslatorImpl.cs b/src/NHibernate/Async/Hql/Ast/ANTLR/QueryTranslatorImpl.cs index a9f8d6a0f80..e36847d2988 100644 --- a/src/NHibernate/Async/Hql/Ast/ANTLR/QueryTranslatorImpl.cs +++ b/src/NHibernate/Async/Hql/Ast/ANTLR/QueryTranslatorImpl.cs @@ -23,6 +23,7 @@ using NHibernate.Hql.Ast.ANTLR.Util; using NHibernate.Loader.Hql; using NHibernate.Param; +using NHibernate.Persister.Collection; using NHibernate.SqlCommand; using NHibernate.Type; using NHibernate.Util; diff --git a/src/NHibernate/Cache/UpdateTimestampsCache.cs b/src/NHibernate/Cache/UpdateTimestampsCache.cs index b6d462328e9..4c6e27885fa 100644 --- a/src/NHibernate/Cache/UpdateTimestampsCache.cs +++ b/src/NHibernate/Cache/UpdateTimestampsCache.cs @@ -55,7 +55,7 @@ public virtual void PreInvalidate(IReadOnlyCollection spaces) } //Since v5.1 - [Obsolete("Please use PreInvalidate(IReadOnlyCollection) instead.")] + [Obsolete("Please use Invalidate(IReadOnlyCollection) instead.")] public void Invalidate(object[] spaces) { //Only for backwards compatibility. diff --git a/src/NHibernate/Hql/Ast/ANTLR/QueryTranslatorImpl.cs b/src/NHibernate/Hql/Ast/ANTLR/QueryTranslatorImpl.cs index 93cbb2afe7b..ed0c71f4de9 100644 --- a/src/NHibernate/Hql/Ast/ANTLR/QueryTranslatorImpl.cs +++ b/src/NHibernate/Hql/Ast/ANTLR/QueryTranslatorImpl.cs @@ -13,6 +13,7 @@ using NHibernate.Hql.Ast.ANTLR.Util; using NHibernate.Loader.Hql; using NHibernate.Param; +using NHibernate.Persister.Collection; using NHibernate.SqlCommand; using NHibernate.Type; using NHibernate.Util; @@ -289,6 +290,23 @@ public bool ContainsCollectionFetches } } + public ISet UncacheableCollectionPersisters + { + get + { + ErrorIfDML(); + var persisters = + ASTUtil.IterateChildrenOfType( + ((QueryNode) _sqlAst).WhereClause, + skipSearchInChildrenWhen: node => node.FromElement != null) + .Select(rn => rn.FromElement) + .Where(fr => fr?.IsFetch == true && fr.QueryableCollection?.HasCache == true) + .Select(fr => fr.QueryableCollection); + + return new HashSet(persisters); + } + } + public bool IsManipulationStatement { get { return _sqlAst.NeedsExecutor; } diff --git a/src/NHibernate/Hql/Ast/ANTLR/Tree/FromClause.cs b/src/NHibernate/Hql/Ast/ANTLR/Tree/FromClause.cs index 4f182051a85..e7e3f25bef6 100644 --- a/src/NHibernate/Hql/Ast/ANTLR/Tree/FromClause.cs +++ b/src/NHibernate/Hql/Ast/ANTLR/Tree/FromClause.cs @@ -9,6 +9,9 @@ namespace NHibernate.Hql.Ast.ANTLR.Tree { + // 6.0 TODO: consider retyping methods yielding IList as IList + // They all do actually yield FromElement, and most of their callers end up recasting + // them. /// /// Represents the 'FROM' part of a query or subquery, containing all mapped class references. /// Author: josh diff --git a/src/NHibernate/Hql/Ast/ANTLR/Util/ASTUtil.cs b/src/NHibernate/Hql/Ast/ANTLR/Util/ASTUtil.cs index 26aba16f094..6e63ee77b5b 100644 --- a/src/NHibernate/Hql/Ast/ANTLR/Util/ASTUtil.cs +++ b/src/NHibernate/Hql/Ast/ANTLR/Util/ASTUtil.cs @@ -103,5 +103,28 @@ public static IList CollectChildren(IASTNode root, FilterPredicate pre { return new CollectingNodeVisitor(predicate).Collect(root); } + + /// + /// Iterates over all children and sub-children and finds elements of required type. + /// + internal static IEnumerable IterateChildrenOfType(IASTNode root, Func skipSearchInChildrenWhen) + { + foreach (var child in root) + { + var searchInChildren = true; + if (child is TRequiredType typedChild) + { + searchInChildren = !skipSearchInChildrenWhen(typedChild); + yield return typedChild; + } + if (searchInChildren) + { + foreach (var subChild in IterateChildrenOfType(child, skipSearchInChildrenWhen)) + { + yield return subChild; + } + } + } + } } -} \ No newline at end of file +} diff --git a/src/NHibernate/Loader/Hql/QueryLoader.cs b/src/NHibernate/Loader/Hql/QueryLoader.cs index f85b2ec4512..a4b91b9e21b 100644 --- a/src/NHibernate/Loader/Hql/QueryLoader.cs +++ b/src/NHibernate/Loader/Hql/QueryLoader.cs @@ -42,6 +42,7 @@ public partial class QueryLoader : BasicLoader private readonly NullableDictionary _sqlAliasByEntityAlias = new NullableDictionary(); private int _selectLength; private LockMode[] _defaultLockModes; + private ISet _uncacheableCollectionPersisters; public QueryLoader(QueryTranslatorImpl queryTranslator, ISessionFactoryImplementor factory, SelectClause selectClause) : base(factory) @@ -279,6 +280,7 @@ private void Initialize(SelectClause selectClause) //NONE, because its the requested lock mode, not the actual! _defaultLockModes = ArrayHelper.Fill(LockMode.None, size); + _uncacheableCollectionPersisters = _queryTranslator.UncacheableCollectionPersisters; } public IList List(ISessionImplementor session, QueryParameters queryParameters) @@ -315,6 +317,11 @@ public override IList GetResultList(IList results, IResultTransformer resultTran } } + protected override bool IsCollectionPersisterCacheable(ICollectionPersister collectionPersister) + { + return !_uncacheableCollectionPersisters.Contains(collectionPersister); + } + protected override IResultTransformer ResolveResultTransformer(IResultTransformer resultTransformer) { return _selectNewTransformer ?? resultTransformer;