Sunday 15 March 2015

concurrency - NHibernate not throwing StaleObjectStateException when <Version> used and data changed in database -


I have been mapped to NHibernate with optimistic concurrency control using an NIT timestamp column as the version number Mapping is like the following:

  & lt; Class name = "entity" optimist-lock = "version" discriminator-value = "0" & ​​gt; & Lt; Id name = "id" & gt; & Lt; Generator class = "parent" /> & Lt; / Id & gt; & Lt; Version name = "version" column = "version" generated = "always" not saved-value = "blank" type = "system.bit [], mscorlib, version = 2.0.0.0, culture = neutral, public token = b77a5c561934e089 "/> ... & lt; Subclass name = "ChildEntity" discriminator-value = "1" /> & Lt; / Square & gt;  

I test what happens when the data in the database is between the data in the row and between the records. To do this, I am running an update statement with a record directly in the table that is in the process of being updated by NHibernate, it directly changes the version number of the records in the update table.

Expected, the NHibernate managed update does not happen on the specific line (this is good), however, no exception is entered during the commitment. I expect a StaleObjectStateException when the transaction was done so that I can roll back the transaction and inform the user. Is not this expected behavior? Am I missing something?

My code looks like this:

  _session.BeginTransaction (); ... Load Objects in the Season: IList & lt; ChildEntity & gt; ToChange = _session.Find ('some status'); Forex Currency (Change Separate Items for Change) {itemToChange.Status = Status.Updated; } ... _session.Transaction.Commit ();  

Items are related to the same session and all tasks are completed within one transaction. Child Entrepreneurship unit is a subclass of base class, in which optimistic lock is set on version.

How are you modifying data? StaleObjectException is thrown only when the NHibernate attempts to update the line and the version number is no longer the other column is irrelevant. Is it possible that you are not updating version number in your test?

This is the basis:

A User A & amp; Get the object from the database with B version = 1

SQL: SELECT [object] from [table] where id = [id] and version = 1

< P> B user A update object which varies in version 2

SQL: UPDATE [TABLE] SET [Object] (& version = 2) where id = [id] and version = 1 1 line updated

C User attempts to update B object, but StaleObjectException is updated with version = 1 Receives as CTO (version Updates 0 record receives the first phase) database.

SQL: UPDATE [TABLE] SET [object] where id = [id] and version = 1 returns 0 rows and stell object findings removed.


No comments:

Post a Comment