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 ?