English 中文(简体)
Version number or timestamp for optimistic concurrency?
原标题:

Would you rather use a version number (that will increment) or a timestamp to check for concurrency problems?

最佳回答

Version number is better because no matter what format of time you use, it can still be thrown off if the server s clock is wrong or becomes wrong over time.

I have to admit, I ve used a time stamp in many databases because it can serve a dual purpose of concurrency checking as well as readability for when the data was last changed. However, using a version number is really the better way to go.

问题回答

I d use a version number, particularly if the resource may ever be updated more than the resolution of your timestamps (e.g. if you re storing timestamps to a resolution of seconds, if you have multiple updates within a single second, your versioning will break).

Lieven,

I know you didn t specify SQL Server, but IF you are talking about SQL Server then the datatype TimeStamp would be the best method to use. Despite its description it doesn t really have anything to do the date and time. It s actually just a binary number which changes every time the row has been modified. Thus, if any amendments are made to the row then you kno the timestamp column will change. This has the advantage over version numbers because you, the programmer, do not have to "maintain" the version number. Actual Date/Time timestamps need to be used more carefully as another poster referred to earlier - time differences etc.

Version number. Or if I m using timestamp, I ll make sure it is UTC - so there is no confusion with the timezone.

If you are on Windows, I recommend to use a globally unique identifier (GUID) as the version identifier.

A timestamp (even if UTC) can make problems if the clock is set by the user. An incrementing number (if hold in memory) can make problems if the server application is restarted, or if it overflows (if it s only a 16-bit or 32-bit integer).

Version number. DateTime is not unique due some reasons, for example Daylight saving time - there could be two 2 AM h. (Ambiguous Time).

I guess that its more than enough unsigned 32 bit integer, because practically there is a ZERO probability that while saving 4,294,967,295 transaction could occur.





相关问题
Need help with web application settings [closed]

Pls give me the solutions for this two problem, which i was asked recently in interview. Prob1: Suppose I have application with 10 admin, so they can change the data anytime their having own userid ...

AutoResetEvent, ManualResetEvent vs Monitor

Lets say I have to orchestrate a synchronization algorithm in .Net 3.5 SP1 and any of the synchronization primitives listed in the title fit perfectly for the task. From a performance perspective, is ...

PHP and Concurrency

I ve been doing some web development work in PHP recently which has led me to study up on the language in general. So far I have not needed to use it to interact with a database, but I know it ...

UI And TcpClient Issue in vb.net

I m having some problems with a small ircbot i m writing. Basically I connect to the server using a tcpclient in a seperate class, which also runs on its own thread. I want to display the server text ...

Prevent Concurrent Editing of a List Item

In Sharepoint MOSS multiple users can edit the same item in a sharepoint list at the same time…the first person to save their edit “wins”. Is there a way to prevent this, to lock the list item while ...

How to properly catch RuntimeExceptions from Executors?

Say that I have the following code: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(myRunnable); Now, if myRunnable throws a RuntimeExcpetion, how can I catch it? ...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I m not sure it ...

热门标签