高并发下membase的数据能否加锁

小木桶 发布于 2011/07/23 11:35
阅读 1K+
收藏 1

怎么锁定membase中的数据啊

在高并发情况下.....

急啊.....................................

加载中
0
C
CuiBin

You can refer to this wiki article:

http://www.couchbase.org/wiki/display/membase/Locking

Locking 


Advisory locks can be useful to control access to scarce resources. For example, retrieving information from backend or remote systems might be slow and consume a lot of resources. Instead of letting any client access the backend system and potentially overwhelm the backend system with high concurrent client requests, you could create an advisory lock to allow only one client at a time access the backend.

Advisory locks in Membase or Memcached can be created by setting expiration times on a named data item and by using the 'add' and 'delete' commands to access that named item. The 'add' or 'delete' commands are atomic, so you can be know that only one client will become the advisory lock owner.

The first client that tries to ADD a named lock item (with an expiration timeout) will succeed. Other clients will see error responses to an ADD command on that named lock item, so they can know that some other client is owning the named lock item. When the current lock owner is finished owning the lock, it can send an explicit DELETE command on the named lock item to free the lock.

If the lock owning client crashes, the lock will automatically become available to the next client that polls for the lock (using 'add') after the expiration timeout.
0
小木桶
小木桶

具体用JAVA怎么操作啊

0
C
CuiBin

Here is an example for .net application. You can use it for java in similar way:

A CAS (Check And Set) operation is the correct way to ensure that operations happen without data loss without requiring distributed locking protocols or transactions to be handled. The way it works is you get the value of something with its cas. This is a monotonically increasing number that can be thought of as the version number of the object in the database. You mutate the object (change it in some way) and use a Cas operation to write it back to the database. This will fail (a casResult whose Result property is set to false) if some other client had modified the value in the database in the meantime. Notice the use of the '??' operator. This says assign to the list variable result.Result if it is not null; otherwise create an empty list

/// <summary>
/// Adds a value to a list of type T associated with the given Key using a
/// CAS operation and retries.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="client"></param>
/// <param name="key"></param>
/// <param name="entry"></param>
private static void AddToListWithCas<T>(MembaseClient client, string key,
                                        T entry)
{
    CasResult<bool> casResult;
    do
    {
        var result = client.GetWithCas<List<T>>(key);
        var list = result.Result ?? new List<T>();
        var cas = result.Cas;

        // Avoid duplicates
        if (list.Contains(entry)) return;

        list.Add(entry);
        casResult = client.Cas(StoreMode.Set, key, list, cas);

    } while (casResult.Result == false);
}

返回顶部
顶部