Search code examples
c#asp.net-web-apiconnection-poolingautofaccouchbase

How to reuse Couchbase .NET SDK connection in Web API requests using Autofac as Dependency Resolver


I am pretty new to Couchbase, and now I am struggling with the basics in reusing the connection to Couchbase Cluster and Bucket.

I got a tip from Couchbase that the ClusterHelper is the way to go forward: https://github.com/couchbase/couchbase-net-client/blob/master/Src/Couchbase/ClusterHelper.cs

The CLusterHelper is included in the latest CouchbaseNetClient v.2.2.0 that I currently use.

My Web.Config section looks like this:

<couchbaseClients>
 <couchbase useSsl="false" operationLifespan="1000">
  <servers>
    <add uri="http://virdrivapp01:8091/pools"></add>
  </servers>
  <buckets>
    <add name="default" useSsl="false" password="" operationLifespan="2000">
      <connectionPool name="custom" maxSize="10" minSize="5" sendTimeout="12000"></connectionPool>
    </add>
  </buckets>
 </couchbase>
</couchbaseClients>

And I can call the ClusterHelper.Initialize method from Global.asax in my Web Api project like this:

ClusterHelper.Initialize("couchbaseClients/couchbase");
_dataStoreClusterPointer = ClusterHelper.Get();
ClusterHelper.GetBucket("default");

The variable _dataStoreClusterPointer is just a static reference I have in Global.asax for testing. Since I use Autofac I have tried to register a static wrapper around the ClusterHelper to as Single Instance.

I have also switched to Autofac in the Web API like this:

 var builder = new ContainerBuilder();

 builder.RegisterType<DataStoreConnection>().SingleInstance();

 AutofacContainerBuilder.RegisterTypes(builder);

 builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

 var container = builder.Build();

 DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
 GlobalConfiguration.Configure(WebApiConfig.Register);
 GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

--- The result?

I get 6 new connections in Couchbase server for every request.

--- Why is it working in my Windows Service then?

That is the strange question. I am also using Autofac and Quartz.NET to run my Windows Services. I use the exact same connection, and I am holding the pointer to the ClusterHelper in a static variable on main thread. And guess what? It works? So I only get 6 connections no matter how many times I use the connection.

--- Does anyone know what I am doing wrong or what I need to do to reuse the connection to Couchbase server using Autofac and Web API?


Solution

  • Well, after talking to Couchbase it seems I get new connections from the server when I do a full index scan on the a large document in one of my N1QL queries.

    So my document is approx. 800 kB size and when that document is part of the index scan it triggers new connections to open no matter if it is .NET, Javascript, CBQ.exe or any other client.

    So the workaround now is to always scope the query down to that document only using USE KEYS.

    More to follow...