Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Nhibernate <version>?

Tags:

nhibernate

I was looking at ayende blog http://ayende.com/blog/3946/nhibernate-mapping-concurrency about NHibernate concurrency and i still not very clear when to use . It seems like it is a solution to solve StaleObjectException.

Can anyone explain to me in what scenario you will use a and why ?

Thanks.

like image 399
TJ. Avatar asked Sep 27 '11 20:09

TJ.


1 Answers

NHibernate Version is used when you want to implement Optimistic concurrency control. Without enabling Optimistic concurrency control and locking your application will use "Last commit wins" strategy. Your users may experience lost updates if two transactions are modifying the same object at roughly the same time. The more appropriate strategy is called "First commit wins". In this scenario second transaction will fail with an error that would say something like: Somebody already committed modifications to the data you’re about to commit. You’ve been working with stale data. Please restart the conversation with fresh data.

From Java Persistence with Hibernate:

Hibernate provides automatic versioning. Each entity instance has a version, which can be a number or a timestamp. Hibernate increments an object’s version when it’s modified, compares versions automatically, and throws an exception if a conflict is detected. Consequently, you add this version property to all your persistent entity classes to enable optimistic locking. ... The version number is just a counter value—it doesn’t have any useful semantic value. The additional column on the entity table is used by your Hibernate application. Keep in mind that all other applications that access the same database can (and probably should) also implement optimistic versioning and utilize the same version column.

like image 110
Dmitry Avatar answered Nov 11 '22 15:11

Dmitry