A: 

There is a session.Lock(object) method.

When you call session.Save(object), NHibernate isn't doing anything in the database until it gets flushed.

Flushing is done (depending on the flush mode, which is usually AutoFlush)

  • before queries (except Get and Load)
  • when calling flush explicitly
  • when committing the transaction (if the connection is created by NH I think)

When the session is flushed, the actual update, insert and delete operations are done on the database and locks are set.

In SQL Server, when the lock is set, the reading transaction is waiting until commit of the updating transaction. When it commits, it reads the committed values (when you are in "Read Committed" isolation).

Stefan Steinegger
I have this for sessions:session.FlushMode = FlushMode.Commit;I know that actual update is done on commit, but what is the point of TransactionScope and its isolation level if locking is done only when database queries are executed? Here is link to SQL Server's documentation about isolation levels:http://msdn.microsoft.com/en-us/library/aa259216(SQL.80).aspxAccording to this, every SELECT that is done inside transaction with Repeatable Read isolation level should place lock on row(s) which is not case in my example.
IvanQ
I may also be a problem with the nested transactions. I don't use this transaction scope - i wrote my own where I know what actually happens ...
Stefan Steinegger
I am thinking of writing my own where I would abstract NHibernate session and transaction, but now I wrote some tests which use NHibernate transaction and result is the same. Locks are acquired only when data is written.
IvanQ
The update is actually done when committing the writing transaction. This is after 20secs. when you call the final scope.Complete is assume. Before, there aren't any locks.
Stefan Steinegger
Yes, I know that, but first select is done inside transaction too and it should be some SELECT FOR UPDATE type. That is the point of using transaction for whole processing, not just for saving. What if some other thread does the same thing? I would have lost update problem.
IvanQ
@IvanQ: you don't get lost updates when you use NHibernate's implements own, powerful optimistic locking mechanism.
Stefan Steinegger
Do you mean versioning?
IvanQ
@IvanQ: either by the version number or a time stamp.
Stefan Steinegger