views:

71

answers:

1

Hi,

I was reading the below blog about hibernate optimistic locking. I am planning to use it with hibernate. But, I have one concern. we have java code and c++ code, both connect to one database. While, java code can use hibernate to achieve optimistic locking, I want to make the c++ code do the same thing. Also, c++ code is using some legacy code.

http://turgaykivrak.wordpress.com/2009/05/16/72/

Is there a documentation that explains how hibernate achieves optimistic locking?

Any suggestions are appreciated.

Thank you
Bala

+5  A: 

To be precise, you don't mean optimistic locking, but optimistic concurrency (without a lock). Using a timestamp for version is just for legacy database support, because a modern database can (at least theoretically) work faster than it's accuracy of storing a timestamp.

Using the integer version property is very simple:

  • On insert: set version to 1
  • On update and delete: increase version with 1 and append "where version=@version" to every sql statementent. Return the number of changed records. Throw a StaleObjectStateException when the number of changed records is different than expected.

Personally, I would not create two separate applications writing the same data in a non-legacy situation, because that means that business logic have to be duplicated and changes have to be applied to two applications, also when the change is relevant for only one of the applications.

Paco
Thank you @Paco. Can I use a bigint instead of integer in mysql.
Algorist
This is correct for MySQL, which is what the question is about, but TimeStamp is preferred if you're using SQL Server. See also http://ayende.com/Blog/archive/2009/04/15/nhibernate-mapping-concurrency.aspx.
Jamie Ide
I don't have experience with the NHibernate version property combined with mysql. I guess you can map mysql bigint to a java long by specifying the type as long in the version property mapping.
Paco
@Jamie Ide: I didn't know that. Thanks for the tip.
Paco