public void QueryIteration() { Prepare(); ISession s = GetSessionUnderTest(); Silly silly = new Silly("silly"); s.Save(silly); s.Flush(); IEnumerable en = s.CreateQuery("from Silly").Enumerable(); IEnumerator itr = en.GetEnumerator(); Assert.IsTrue(itr.MoveNext()); Silly silly2 = (Silly)itr.Current; Assert.AreEqual(silly, silly2); NHibernateUtil.Close(itr); itr = s.CreateQuery("from Silly").Enumerable().GetEnumerator(); IEnumerator itr2 = s.CreateQuery("from Silly where name = 'silly'").Enumerable().GetEnumerator(); Assert.IsTrue(itr.MoveNext()); Assert.AreEqual(silly, itr.Current); Assert.IsTrue(itr2.MoveNext()); Assert.AreEqual(silly, itr2.Current); NHibernateUtil.Close(itr); NHibernateUtil.Close(itr2); s.Delete(silly); s.Flush(); Release(s); Done(); }
public void SerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources() { TestsContext.AssumeSystemTypeIsSerializable(); Prepare(); ISession s = GetSessionUnderTest(); Silly silly = new Silly("silly"); s.Save(silly); // this should cause the CM to obtain a connection, and then Release it s.Flush(); // both scroll() and iterate() cause the batcher to hold on // to resources, which should make aggresive-Release not Release // the connection (and thus cause serialization to fail) IEnumerable en = s.CreateQuery("from Silly").Enumerable(); try { SerializationHelper.Serialize(s); Assert.Fail( "Serialization allowed on connected session; or aggressive Release released connection with open resources"); } catch (InvalidOperationException) { // expected behavior } // Closing the ScrollableResults does currently force the batcher to // aggressively Release the connection NHibernateUtil.Close(en); SerializationHelper.Serialize(s); s.Delete(silly); s.Flush(); Release(s); Done(); }
public void QueryStatGathering() { IStatistics stats = Sfi.Statistics; stats.Clear(); ISession s = OpenSession(); ITransaction tx = s.BeginTransaction(); FillDb(s); tx.Commit(); s.Close(); s = OpenSession(); tx = s.BeginTransaction(); string continents = "from Continent"; int results = s.CreateQuery(continents).List().Count; QueryStatistics continentStats = stats.GetQueryStatistics(continents); Assert.IsNotNull(continentStats, "stats were null"); Assert.AreEqual(1, continentStats.ExecutionCount, "unexpected execution count"); Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count"); var maxTime = continentStats.ExecutionMaxTime; Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime); Assert.AreEqual(continents, stats.QueryExecutionMaxTimeQueryString); IEnumerable itr = s.CreateQuery(continents).Enumerable(); // Enumerable() should increment the execution count Assert.AreEqual(2, continentStats.ExecutionCount, "unexpected execution count"); // but should not effect the cumulative row count Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count"); NHibernateUtil.Close(itr); tx.Commit(); s.Close(); // explicitly check that statistics for "split queries" get collected // under the original query stats.Clear(); s = OpenSession(); tx = s.BeginTransaction(); string localities = "from Locality"; results = s.CreateQuery(localities).List().Count; QueryStatistics localityStats = stats.GetQueryStatistics(localities); Assert.IsNotNull(localityStats, "stats were null"); // ...one for each split query Assert.AreEqual(2, localityStats.ExecutionCount, "unexpected execution count"); Assert.AreEqual(results, localityStats.ExecutionRowCount, "unexpected row count"); maxTime = localityStats.ExecutionMaxTime; Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime); Assert.AreEqual(localities, stats.QueryExecutionMaxTimeQueryString); tx.Commit(); s.Close(); Assert.IsFalse(s.IsOpen); // native sql queries stats.Clear(); s = OpenSession(); tx = s.BeginTransaction(); string sql = "select Id, Name from Country"; results = s.CreateSQLQuery(sql).AddEntity(typeof(Country)).List().Count; QueryStatistics sqlStats = stats.GetQueryStatistics(sql); Assert.IsNotNull(sqlStats, "sql stats were null"); Assert.AreEqual(1, sqlStats.ExecutionCount, "unexpected execution count"); Assert.AreEqual(results, sqlStats.ExecutionRowCount, "unexpected row count"); maxTime = sqlStats.ExecutionMaxTime; Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime); Assert.AreEqual(sql, stats.QueryExecutionMaxTimeQueryString); tx.Commit(); s.Close(); s = OpenSession(); tx = s.BeginTransaction(); CleanDb(s); tx.Commit(); s.Close(); }
public async Task QueryStatGatheringAsync() { IStatistics stats = Sfi.Statistics; stats.Clear(); ISession s = OpenSession(); ITransaction tx = s.BeginTransaction(); await(FillDbAsync(s)); await(tx.CommitAsync()); s.Close(); s = OpenSession(); tx = s.BeginTransaction(); string continents = "from Continent"; int results = (await(s.CreateQuery(continents).ListAsync())).Count; QueryStatistics continentStats = stats.GetQueryStatistics(continents); Assert.IsNotNull(continentStats, "stats were null"); Assert.AreEqual(1, continentStats.ExecutionCount, "unexpected execution count"); Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count"); var maxTime = continentStats.ExecutionMaxTime; Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime); Assert.AreEqual(continents, stats.QueryExecutionMaxTimeQueryString); IEnumerable itr = await(s.CreateQuery(continents).EnumerableAsync()); // Enumerable() should increment the execution count Assert.AreEqual(2, continentStats.ExecutionCount, "unexpected execution count"); // but should not effect the cumulative row count Assert.AreEqual(results, continentStats.ExecutionRowCount, "unexpected row count"); NHibernateUtil.Close(itr); await(tx.CommitAsync()); s.Close(); // explicitly check that statistics for "split queries" get collected // under the original query stats.Clear(); s = OpenSession(); tx = s.BeginTransaction(); string localities = "from Locality"; results = (await(s.CreateQuery(localities).ListAsync())).Count; QueryStatistics localityStats = stats.GetQueryStatistics(localities); Assert.IsNotNull(localityStats, "stats were null"); // ...one for each split query Assert.AreEqual(2, localityStats.ExecutionCount, "unexpected execution count"); Assert.AreEqual(results, localityStats.ExecutionRowCount, "unexpected row count"); maxTime = localityStats.ExecutionMaxTime; Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime); Assert.AreEqual(localities, stats.QueryExecutionMaxTimeQueryString); await(tx.CommitAsync()); s.Close(); Assert.IsFalse(s.IsOpen); // native sql queries stats.Clear(); s = OpenSession(); tx = s.BeginTransaction(); string sql = "select Id, Name from Country"; results = (await(s.CreateSQLQuery(sql).AddEntity(typeof(Country)).ListAsync())).Count; QueryStatistics sqlStats = stats.GetQueryStatistics(sql); Assert.IsNotNull(sqlStats, "sql stats were null"); Assert.AreEqual(1, sqlStats.ExecutionCount, "unexpected execution count"); Assert.AreEqual(results, sqlStats.ExecutionRowCount, "unexpected row count"); maxTime = sqlStats.ExecutionMaxTime; Assert.AreEqual(maxTime, stats.QueryExecutionMaxTime); Assert.AreEqual(sql, stats.QueryExecutionMaxTimeQueryString); // check that 2nd query correctly updates query statistics results = (await(s.CreateSQLQuery(sql).AddEntity(typeof(Country)).ListAsync())).Count; var queryTime1 = maxTime; var queryTime2 = sqlStats.ExecutionMaxTime == maxTime ? sqlStats.ExecutionMinTime : sqlStats.ExecutionMaxTime; Assert.AreEqual(2, sqlStats.ExecutionCount, "unexpected execution count"); Assert.AreEqual((queryTime1 + queryTime2).Ticks / 2, sqlStats.ExecutionAvgTime.Ticks, 2, "unexpected average time"); await(tx.CommitAsync()); s.Close(); s = OpenSession(); tx = s.BeginTransaction(); await(CleanDbAsync(s)); await(tx.CommitAsync()); s.Close(); }