I think our database is down



  • Public Class clsGlobalConnection
        Public Shared gServer As String
        Public Shared gDatabase As String
        Public Shared gUsername As String
        Public Shared gConnection As New SqlConnection()
        Public Shared Function ConnectToDatabase(ByVal gstrServer As String, ByVal gstrDatabase As String, _
     ByVal gstrUsername As String) As Boolean

            gServer = gstrServer
            gDatabase = gstrDatabase
            gUsername = gstrUsername

            If gConnection Is Nothing Then
                gConnection = New SqlConnection("user id=" & gUsername & ";password=<password>;initial catalog=" & gDatabase & ";data source=" & gServer & ";")
                gConnection.Open()
            End If
        End Function
    End Class

     I have seen some interesting things at my current assignment, but this one was interesting.  The "want to be <FONT size=2>architect" for this organization was proactive and took it upon themself to convert an IT application into VB.Net 2005.  Once it was complete, the application did not working.   At quick glance, this code looks OK, but how often is the gConnection going to be Nothing? 

    </FONT>


  • @djgerend said:

    <FONT size=2>... but how often is the gConnection going to be Nothing?  </FONT>

    By my maths, once every blue moon, a month of Sunday's after hell freezes over.
    Alternatively, since gConnection is public and shared, whenever some dumbass junior sets it to nothing outside of the function!



  • How often? 

    1. When the class is first instanciated. 

    2. If a close method was included that clears the connection and sets it to nothing.  Usually used for resetting a connection for some reason without destroying the class.

    3. Since these are shared objects, anything can clear the connection and it would be proper to check for this whenever you try to open a new connection.

    4. The real WTF is why the hell all this is shared?  If you are going to have a connection class, then initialize the class at startup but let it manage it's own objects so nothing else messes with it.  This object will have no idea what state it is in at any time, hence the above code is necessary. 



  • @KattMan said:

    How often? 

    1. When the class is first instanciated. 

    2. If a close method was included that clears the connection and sets it to nothing.  Usually used for resetting a connection for some reason without destroying the class.

    3. Since these are shared objects, anything can clear the connection and it would be proper to check for this whenever you try to open a new connection.

    4. The real WTF is why the hell all this is shared?  If you are going to have a connection class, then initialize the class at startup but let it manage it's own objects so nothing else messes with it.  This object will have no idea what state it is in at any time, hence the above code is necessary. 

    1. The connection is set to a new connection, which does not equal nothing (I am pretty sure at least?)

    2. I see a start and end of the class so I am guessing such a method does not exist.

    3. Okay this would be true, but why do this outside your class? Your close or check to see if the connection is open would probably work better.

    4. Agreed.



  • A connection for life?



  • Problem seems to be that it is initialised with a New SqlConnection(), so it will never be Nothing, unless some doofus sets it to Nothing, as Martin said.



  • @Treeki said:

    Problem seems to be that it is initialised with a New SqlConnection(), so it will never be Nothing, unless some doofus sets it to Nothing, as Martin said.

     

    The WTF is not the test. Since the "designer" made the connection object a shared member, anything could nullify it. This is a good sensible test in an otherwise poorly designed class. There's no way to know what someone else will do with that publicly exposed member. Actually, it can be inferred that someone MUST be manipulating it outside, since the class just creates a new connection object with no parameters, something SOMEWHERE must be accessing the object to at least set connection criteria. So if that's being done, who knows what else is being done.


     

     

     

     



  • @matthewr81 said:

    @KattMan said:
    How often? 

    1. When the class is first instanciated. 

    2. If a close method was included that clears the connection and sets it to nothing.  Usually used for resetting a connection for some reason without destroying the class.

    3. Since these are shared objects, anything can clear the connection and it would be proper to check for this whenever you try to open a new connection.

    4. The real WTF is why the hell all this is shared?  If you are going to have a connection class, then initialize the class at startup but let it manage it's own objects so nothing else messes with it.  This object will have no idea what state it is in at any time, hence the above code is necessary. 

    1. The connection is set to a new connection, which does not equal nothing (I am pretty sure at least?)

    2. I see a start and end of the class so I am guessing such a method does not exist.

    3. Okay this would be true, but why do this outside your class? Your close or check to see if the connection is open would probably work better.

    4. Agreed.

    Yeah. the first point I will give you, I missed that.

    The second point, I can only assume this might be a snipped version of the class, that why I said "if".  I doubt he would include the whole class if there were a bunch of other methods. 



  • This the entire class and there is not a close method.

    It appears it was implemented without testing it.   Clearly, if they would of tested it, they would of found the flaw with the design.



  • @unklegwar said:

    There's no way to know what someone else will do with that publicly exposed member.

    You can never appreciate how often I've heard that phrase spoken. 



  • @misguided said:

    @unklegwar said:

    There's no way to know what someone else will do with that publicly exposed member.

    You can never appreciate how often I've heard that phrase spoken. 

    I never write LOL. But...

     

     

    LOL!

     

     

     

     



  • @Martin said:

    Alternatively, since gConnection is public and shared, whenever some dumbass junior sets it to nothing outside of the function!

    It's been a long time since I did anything with VB, and if this is .NET it might have changed, but IIRC if a variable is declared "As New (something)", the compiler will insert a check for Nothingness before every access, and automatically assign a new object if necessary.



  •  

    I was taught, "If you expose it, they will use it, if they use it, you must maintain it. "   

    I try to keep as many things hidden as I can to ensure that it is used the way it was intended.



  • @djgerend said:

     

    I was taught, "If you expose it, they will use it, if they use it, you must maintain it. "   

    I try to keep as many things hidden as I can to ensure that it is used the way it was intended.

    True in so many, many ways. 



  • @iwpg said:

    @Martin said:

    Alternatively, since gConnection is public and shared, whenever some dumbass junior sets it to nothing outside of the function!

    It's been a long time since I did anything with VB, and if this is .NET it might have changed, but IIRC if a variable is declared "As New (something)", the compiler will insert a check for Nothingness before every access, and automatically assign a new object if necessary.

     From what I've read, this changed in .NET - "As New (something)" is now syntactic sugar for "As (something) = New (something)"
     



  • @Random832 said:

    @iwpg said:
    @Martin said:

    Alternatively, since gConnection is public and shared, whenever some dumbass junior sets it to nothing outside of the function!

    It's been a long time since I did anything with VB, and if this is .NET it might have changed, but IIRC if a variable is declared "As New (something)", the compiler will insert a check for Nothingness before every access, and automatically assign a new object if necessary.

     From what I've read, this changed in .NET - "As New (something)" is now syntactic sugar for "As (something) = New (something)"
     

     

    It is. In VB.Net gone are the days of object being magically instantiated whenever you look at them (as it was in VB6. We tend to call bugs coming from that particular "feature" heisenbugs here).

    Dim b as New MyClass is indeed just syntactic sugar for Dim b as MyClass = New MyClass



  • @Denis Troller said:

    @Random832 said:

    From what I've read, this changed in .NET - "As New (something)" is now syntactic sugar for "As (something) = New (something)"

    It is. In VB.Net gone are the days of object being magically instantiated whenever you look at them (as it was in VB6. We tend to call bugs coming from that particular "feature" heisenbugs here).

    Dim b as New MyClass is indeed just syntactic sugar for Dim b as MyClass = New MyClass

    Fair enough.


Log in to reply