0

I have a simple VB.net windows form application. I have a method for opening the connection to the database. I am using two MySql connections. In the method below it can be seen that My primary connection is opened and after that my secondary connection is opened. Both connections have different connection strings but in very rare cases these connection strings can be same so in that I don't want to open the connection twice.

Dim MySqlConnPrimary As New MySqlConnection
Dim MySqlConnSecondary As New MySqlConnection

Public Sub OpenConnection(ByVal strConnectionStringPrimary As String, ByVal strConnectionStringSecondary As String)

    Try
        With MySqlConnPrimary
            .ConnectionString = strConnectionStringPrimary
            .Open()
        End With

        With MySqlConnSecondary
            .ConnectionString = strConnectionStringSecondary
            .Open()
        End With

    Catch ex As Exception

    End Try
End Sub

Mostly the connection strings will be different but in case the connection strings are same then I don't want to open the connections multiple times. How should I achieve this? Should I assign MySqlConnPrimary to MySqlConnSecondary incase the connection strings are same?

4
  • You can check the connection State by using Property Named : MySqlConnPrimary.ConnectionState Check it using if..else block and code the necessary actions according to it. Commented Aug 11, 2015 at 8:03
  • @Mahadev Thank you for your quick response. What I want is that If the connection strings are same then both MySqlConnPrimary and MySqlConnSecondary should use 1 connection so that they are opened just once. Commented Aug 11, 2015 at 8:07
  • You should not keep a connection open; instead, open, do operation, close. That might eliminate the problem. (Connection pooling is used to make it efficient.) Commented Aug 11, 2015 at 8:09
  • 2
    Be wary of comparing connection strings. Two semantically identical connection strings could be unequal with regard to pure string comparison. Commented Aug 11, 2015 at 8:10

1 Answer 1

1

The obvious and simple answer is to string compare the connection strings and reuse the connection if they are the same.

In pseudo-code:

If string.Equals(strConnectionStringPrimary,strConnectionStringSecondary)
    MySqlConnSecondary = MySqlConnPrimary
Else
    With MySqlConnSecondary
        .ConnectionString = strConnectionStringSecondary
        .Open()
    End With
End If

It doesn't feel like an ideal solution, however it is a mechanism to answer your immediate question.

Sign up to request clarification or add additional context in comments.

2 Comments

@J R Thanks. Thats what I was looking for. I was thinking the same but was not sure whether it is the elegant way of achieving.
I don't think there's an elegant way to solve the immediate problem that you're asking for help with, outside of a reconsideration of your architecture. The usual paradigm is to open a connection, perform some work, and then close the connection. The other note I'd leave is to not just use the "==" operator, but an operator appropriate for your workflow - perhaps the string.Compare(string,string) method, if you choose to keep your current methodology

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.