0

I have a number of pages that run 10 t0 15 different queries to build up a page. I see from a few articles, to have nhibernate second level cache working I need to use transactions.

So I want to confirm that i should be putting all of those 10 to 15 queries inside a single transaction? something like this?

    var session = Repository.Session;
    using (var tx = session.BeginTransaction())
     {
           var viewModel = new ViewModel();
           viewModel.Cars  = session.Query<Car>();
           viewModel.Dogs = session.Query<Dog>();
           viewModel.Cats  = session.Query<Cat>();
           viewModel.Birds  = session.Query<Bird>();
            tx.Commit();
      }
leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

1

The queries do not need to be executed in the same transaction but there's no reason not to. That is, it's not necessary but it's more work to do it any other way.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • what is odd is that when testing with nhibernate profiler i tend to agree with you BUT from a lot of documentation i have read, like this question: http://stackoverflow.com/questions/10226461/nhibernate-second-level-cache-with-implicit-transactions it seems its saying that you DO need to put things in a transactions to get second level caching . . that is why i am a bit confused . . – leora Apr 09 '14 at 15:52
  • As far as I know, you do need to make the reads in an ITransaction. My answer is that they don't have to be in a SINGLE transaction which is how I interpreted your question. – Jamie Ide Apr 09 '14 at 16:02
  • ah gotcha . . my point is still the same (that i see it working even without any transaction), that being said to your last comment why would you want to create multiple transactions given the use case listed in the question – leora Apr 09 '14 at 16:12