Thanks, I read it. The closest it comes to answering my question is the following paragraph:
Quote:
Transactions also group data access operations, in fact, every SQL statement, be it queries or DML, has to execute inside a database transaction. There can be no communication with a database outside of a database transaction. (Note that there are such things as read-only transactions, that can be used to improve cleanup time in a database engine if it is not smart enough to optimize its own operations.)
This implicitly implies that Hibernate will create a transaction even if the calling code doesn't. But I am still curious:
1) Which way is more efficient?
2) Is the answer the same for NHibernate (which is the platform for my original question)? I read
http://www.hibernate.org/hib_docs/nhibernate/html/transactions.html but it does not address the issue.
So to clarify my question, which of the following two options is the preferred way to execute read-only transactions in NHibernate? Are they logically the same?
Option 1
Code:
ISession session = sessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();
try
{
IList objects = session.CreateQuery("...").List();
tx.Commit();
}
catch (Exception)
{
tx.Rollback();
throw;
}
finally
{
session.Close();
}
Option 2Code:
ISession session = sessionFactory.OpenSession();
try
{
IList objects = session.CreateQuery("...").List();
}
finally
{
session.Close();
}