Could not acquire server exception, can't connect to server all the time

I looked at the response here. My first query request works and the second one and future query request won’t work because it throws that exception. I believe it does have something to do with maintain a reference to the cluster object and going out of scope as jmorris mentioned.

I have a class that connects to couchbase server, the bucket name, pw and uri’s are stored in application setting in web.config:

public class DatabaseHelper
    {
        public static IBucket __Bucket;//changed this to static

        public static void Connect()//changed this to static
        {
            List<Uri> _servers = new List<Uri>();
            foreach (string _srv in Properties.Settings.Default.CouchServers.Cast<string>().ToList())
               _servers.Add(new Uri(_srv));

             Properties.Settings.Default.CouchServers.Cast<string>().ToList().ForEach(_srv =>
            {
                _servers.Add(new Uri(_srv));
            });
            ClusterHelper.Initialize(new ClientConfiguration
            {
                Servers = _servers,
                BucketConfigs = new Dictionary<string, BucketConfiguration>
                {
                    {
                        Properties.Settings.Default.CouchBucket, new BucketConfiguration
                        {
                            BucketName = Properties.Settings.Default.CouchBucket,
                            Password = Properties.Settings.Default.BucketPassword
                        }
                    }
                }
            });

            __Bucket = ClusterHelper.GetBucket(Properties.Settings.Default.CouchBucket);
        }

example get method:

public ActionResult ReturnUldInformation()
{
    //DatabaseHelper dbHelper = new DatabaseHelper();
    try
    {
        //dbHelper.Connect();
        DatabaseHelper.Connect();
        var _variableList= new List<modelOfSomething>();

        {
            string query = "SELECT `BucketName`.* FROM `BucketName` WHERE type = 'typeINeed' ";
            var queryRequest = new QueryRequest(query);
            //var result = dbHelper.__Bucket.Query<dynamic>(queryRequest);
            var result = DatabaseHelper.__Bucket.Query<dynamic>(queryRequest);
            foreach (var row in result.Rows)
            {
                var objectReference= new modelOfSomething()
                {
                    SerialNumber = row.serialNumber,
                };

                _variableList.Add(objectReference);
            }
        }
        return View();
    }
    finally
    {
          //dbHelper.Close();; use to have this before changing the connection to static. Now I don't close after every method call
    }
}

I use to call this method in the DatabaseHelper class

public void Close()
{
    ClusterHelper.Close();
}

I was wondering what I could do fix this issue.

@krnforsale -

You should be calling Connect() when the application starts up and Close() when the application shuts down…for ASP.NET this mean hooking into Application_Start and Application_End in Global.asax or in Startup.cs for OWIN based servers. In general, you’ll need to keep the client connections open the entire lifespan of the application and not open/close them per HTTP request.

-Jeff

1 Like