Logo

NHibernate

The object-relational mapper for .NET

NHibernate’s Future Queries And Their Fallback Behavior

I've blogged about NHibernate's Future queries a couple of times already. But as you know, NHibernate aims to offer you a way to write your code completely independent of the actual database you're using. So what happens if you run your code, which is using the Future and FutureValue features, on a database that doesn't support batched queries? Previously, this would fail with a NotSupportedException being thrown.

As of today, (revision 4177 if you want to be specific) this is no longer the case. If you use the Future or FutureValue methods of either ICriteria or IQuery, and the database doesn't support batching queries, NHibernate will fall back to simply executing the queries immediately, as the following tests show:

        [Test]

        public void FutureOfCriteriaFallsBackToListImplementationWhenQueryBatchingIsNotSupported()

        {

            using (var session = sessions.OpenSession())

            {

                var results = session.CreateCriteria<Person>().Future<Person>();

                results.GetEnumerator().MoveNext();

            }

        }

        [Test]

        public void FutureValueOfCriteriaCanGetSingleEntityWhenQueryBatchingIsNotSupported()

        {

            int personId = CreatePerson();

 

            using (var session = sessions.OpenSession())

            {

                var futurePerson = session.CreateCriteria<Person>()

                    .Add(Restrictions.Eq("Id", personId))

                    .FutureValue<Person>();

                Assert.IsNotNull(futurePerson.Value);

            }

        }

There are more tests obviously, but you get the point. The interesting part about these tests is how i disabled query batching support. I only have Sql Server and MySQL running on this machine, and they both support query batching. I didn't really feel like installing a database that doesn't support it, so i just took advantage of NHibernate's extensibility. Since most of us run the NHibernate tests on Sql Server, i inherited from the Sql Server Driver and made sure that it would report to NHibernate that it didn't support query batching:

    public class TestDriverThatDoesntSupportQueryBatching : SqlClientDriver

    {

        public override bool SupportsMultipleQueries

        {

            get { return false; }

        }

    }

Easy huh? Then i just inherited from the TestCase class we have in the NHibernate.Tests project which offers a virtual method where you can modify the NHibernate configuration for the current fixture:

        protected override void Configure(Configuration configuration)

        {

            configuration.Properties[Environment.ConnectionDriver] =

                "NHibernate.Test.NHSpecificTest.Futures.TestDriverThatDoesntSupportQueryBatching, NHibernate.Test";

            base.Configure(configuration);

        }

Now NHibernate thinks that query batching isn't supported, yet the above tests still work. Mission accomplished :)


Posted Mon, 13 April 2009 09:09:00 AM by DavyBrion
Filed under:

comments powered by Disqus
© NHibernate Community 2024