views:

437

answers:

3

Hello there,

I am new to Nhibernate, so my query may seem trivial to you.

We generally embed data operation code inside

using (var session = sessionFactory.OpenSession())

{

using (var transaction = session.BeginTransaction())
{
   ...Code for CRUD operations
   transaction.Commit();
}

}

Because we generally BeginTransaction/Commit/Rollback for saving/updating/deleting data,

I wonder if BeginTransaction() and Commit() are required even if I am retriving data using session.Get(id); or session.CreateCriteria().List();

Please guide.

Thank you!

+1  A: 

No, you can't do data retrieval without transaction; all NH operations are transaction-centric.

Read this article by Ayende Rahien.

o.k.w
But doesn't he say that you should use transactions for every operation in NH? Why do you say "No"?
HeavyWave
@HeavyWave: I meant "No, you can't do that without transaction", guess I read the line wrongly.
o.k.w
A: 

You should always use transactions in quieries. Not only for the perfomance but also for concurrency reasons, and Ayende says about it.

His Hibernate profiler shows and alert if you use queries not in transaction scope.

Sly
A: 

Some tips:

  • Putting the language and package you're using in the title or in the tags helps other people with more experience find your question easier.
  • Put four spaces in front of your code to indent it, like this:


main()
{
   printf("Hello World!\n");
   return 0;
}

I hope you continue to enjoy using Stack Overflow!

knight666