同时使用couchbase客户端到同一个couchbase服务器的问题

时间:2013-11-26 07:09:50

标签: .net client couchbase

我知道来自couchbase的建议是创建一个couchbase客户端的单个实例,并在整个应用程序中重用它。但在我的情况下,创建几个更容易,因为它们可能连接到几个不同的基础服务器,并且在这种情况下创建实例的时间使用不是一个因素。但是,似乎向同一服务器创建两个实例失败。请考虑以下代码:

var section = (CouchbaseClientSection)<Section of configuration read from web.config>;
var client1 = new CouchbaseClient(section);
//a call to fetch data from client1 here succeeds just fine
var client2 = new CouchbaseClient(section);
//a call to fetch data from client2 here fails with the following message:
//{"Operation is not valid due to the current state of the object."}

稍作修改,并在创建第二个客户端之前处置第一个客户端,它可以正常工作:

var section = (CouchbaseClientSection)<Section of configuration read from web.config>;
var client1 = new CouchbaseClient(section);
//a call to fetch data from client1 here succeeds just fine
client1.Dispose();
var client2 = new CouchbaseClient(section);
//a call to fetch data from client2 here succeeds just fine

其次,如果client2指向另一个coiuchbase实例,它也可以正常工作:

var section1 = (CouchbaseClientSection)<Section of configuration read from web.config>;
var section2 = (CouchbaseClientSection)<Another section of configuration read from web.config>;
var client1 = new CouchbaseClient(section1);
//a call to fetch data from client1 here succeeds just fine
var client2 = new CouchbaseClient(section2);
//a call to fetch data from client2 here succeeds just fine

因此,如果有两个活动连接同时向同一个couchbase实例打开,它只会失败,但我在couchbase文档中找不到任何内容,表明这是一个问题。 为什么第一个示例失败?

有人和我有同样的问题吗?可能有某些设置需要更改以允许这个吗?

1 个答案:

答案 0 :(得分:0)

使用1.3.0版本的客户端可以使用AFAIK:

var section = ConfigurationManager.GetSection("couchbase") as CouchbaseClientSection;

var client1 = new CouchbaseClient(section);
var result1 = client1.ExecuteStore(StoreMode.Set, "xxxfnkey1", "xxxfnvalue1");
Console.WriteLine("Result1: {0} {1}", result1.Success, result1.Message);

var getResult1 = client1.ExecuteGet("xxxfnkey1");
Console.WriteLine("getResult1: {0} {1}", getResult1.Success, getResult1.Message);

var client2 = new CouchbaseClient(section);
var result2 = client2.ExecuteStore(StoreMode.Set, "xxxfnkey2", "xxxfnvalue2");
Console.WriteLine("Result2: {0} {1}", result2.Success, result2.Message);

var getResult2 = client2.ExecuteGet("xxxfnkey2");
Console.WriteLine("getResult2: {0} {1}", getResult2.Success, getResult2.Message);

如果它不适合您,则可能是您的配置问题。客户端本身肯定支持从单个配置部分创建多个实例。