1

I am using Couchbase in .NET (VB) and i am trying to come up with a way to reuse my Cluster connection. My class has a Function called couchInit() with the folowing code.

Public Class couchBase
    Public cbCluster As New Cluster

    Public Function couchInit() As Cluster
        Try
           Dim cluster As New Cluster(New ClientConfiguration With {
                    .Servers = New List(Of Uri) From {
                    New Uri(ConfigurationManager.AppSettings("couchServer").ToString())}})

                Dim authenticator = New PasswordAuthenticator(ConfigurationManager.AppSettings("couchUser").ToString(), ConfigurationManager.AppSettings("couchPassword").ToString())
                cluster.Authenticate(authenticator)

                System.Diagnostics.Debug.WriteLine("Open Connection")

                cbCluster = cluster
            End If
        Catch ex As Exception
            System.Diagnostics.Debug.WriteLine(ex)
        End Try
    End Function

    Public Function getDoc(ByVal docID As String)
        Try
            'TODO Check if Bucket is active
            If (cbCluster.IsOpen(ConfigurationManager.AppSettings("couchBucket").ToString()) = False) Then
                couchInit()
            End If

            Dim bucket = cbCluster.OpenBucket(ConfigurationManager.AppSettings("couchBucket").ToString())
            System.Diagnostics.Debug.WriteLine("Open Bucket")
            Dim Doc = bucket.GetDocument(Of Object)(docID)
            Dim myObj As Object = Doc.Content
            System.Diagnostics.Debug.WriteLine(Doc)
            System.Diagnostics.Debug.WriteLine(cbCluster.IsOpen(ConfigurationManager.AppSettings("couchBucket").ToString()))

            Return Doc
        Catch ex As Exception

            System.Diagnostics.Debug.WriteLine(ex)
        End Try
     End Function
 End Class

All good so far i can access my database but every time i call this function from another function like this

Dim couch = New couchBase
couch.getDoc("uriEndPoint::C985544D-2A31-44A0-8228-3318A56DB8E9")

my code creates a new Cluster as it things its closed, what am i missing here ?

NoSoup4you
  • 640
  • 1
  • 10
  • 34
  • When you create a new couch object (Dim couch = New couchBase) then the cbCluster object in the new couch object has not yet been opened. – Honeyboy Wilson Mar 12 '20 at 21:16
  • I figured that much, is there a way to prevent this to make my cluster avail without creating a new one every time.. – NoSoup4you Mar 12 '20 at 21:53
  • 1
    You could create just one globally accessible couch object and call couchInit one time. – Honeyboy Wilson Mar 12 '20 at 23:04
  • Is this your own duplicate? [How can i initialize my DB connection on start of Webservice](https://stackoverflow.com/questions/60659630/how-can-i-initialize-my-db-connection-on-start-of-webservice) – T.S. Mar 13 '20 at 01:36

1 Answers1

1

I wouldn't recommend you do this. A Cluster is something you should create once and reuse for the whole app (like a singleton). I don't see any IDisposable here. A Cluster is disposable, but your wrapper object isn't. So every time you create a new wrapper, you have the potential to leave a cluster connection undisposed when your wrapper is garbage collected (this is true of all database connections I've used in my career, not just Couchbase by the way).

Instead, there is a ClusterHelper that will manage the cluster for you. Use ClusterHelper.Initialize once during startup, and then ClusterHelper.GetBucket from that point on. I'd recommend doing that. This has a bonus of simplifying your wrapper objects too.

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121