In the previous version of the sdk I had to write a helper singleton class to keep alive connections to the cluster bucket. In the new sdk is this still necessary?
There is a singleton cluster object that comes with the SDK http://docs.couchbase.com/sdk-api/couchbase-net-client-2.1.0/html/01681e62-80ff-97c3-a82f-45cd379f39f3.htm
Note that you open multiple buckets (by name) using it and it will manage the resources for you.
In an ASP.NET application, you can use it like this:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//initialize the clusterhelper
ClusterHelper.Initialize();
}
protected void Application_End()
{
//close it down
ClusterHelper.Close();
}
}
In your controller you just get and use the bucket (but don’t dispose of it):
public async Task<ActionResult> Index()
{
var bucket = ClusterHelper.GetBucket("default");
var result = await bucket.ExistsAsync("1000").ConfigureAwait(false);
return View();
}
You just keep reusing the bucket instance, of which a reference is stored within the ClusterHelper.
-Jeff
Here is a snippet showing how to configure the ClusterHelper:
https://gist.githubusercontent.com/martinesmann/741ed84610a7ee114b72/raw/7a9ee8ec9ba828fa9cb9b54ed82bdc37d04650f2/Program(final).cs
Thanks. I have already configured a singleton class for my connection pooling from the last version of the SDK. In the near future I will migrate.
If you need inspiration to implement the Singleton class for ClusterHelper or would like to tweak it a bit you can find the source here:
One thing to notice is the call to Dispose of bucket’s just after they are removed from the internal bucket’s list.