Can an exception cause the closing of a SqlConnection?

2

I am building the connection treatment and this question came to mind, even though I could not find a satisfactory answer. So I came here to ask the most experienced programmers.

Is there any possibility that when a exception is thrown a SqlConnection already opened is closed because of this exception ?

Illustrative code:

SqlConnection con = new SqlConnection(connectionString);

        try
        {
            con.Open();

            //algum código aqui....
        }
        catch (Exception)
        {
            //possibilidade de fechamento da conexão? .....
            throw;
        }
    
asked by anonymous 06.01.2016 / 17:06

2 answers

3

Although the ideal is not to capture Exception because it is very generic and throw the exception again, it is not the most appropriate thing to do, yes, it is possible to close the connection there . Only it probably is not what you want. You want the connection to always be closed, right? If all goes well or an exception is made, the connection must be closed at the end of the process. The language has a standard that guarantees it in a much simpler way:

using (var con = new SqlConnection(connectionString)) {
    con.Open();
    //algum código aqui....
}

There you are, you're closing, you do not need anything else. The using will produce a try-finally by calling the dispose() that will be responsible for closing the connection. Ideally, do not try to do it at hand. This is the right and risk-free way.

The connection will hang if you do not do this. This holds true for any external resource that is purchased. Every time you "open" something, you need to ensure that it will be closed, closing is not automatic. This pattern helps achieve that goal the right way.

06.01.2016 / 17:16
1

You're creating the variable inside try so it's not visible outside of it, I'll put it as it would be to use the outside variable because this holds for any variable, not just for the connection case:

SqlConnection con
 try
        {
            con = new SqlConnection(connectionString);

            con.Open();

            //algum código aqui....
        }
        catch (Exception)
        {
            con.Close()
            throw;
        }

There are other ways to use this, even better ways to treat it, the best would be something like this: ( code below removed from that link

using (SqlConnection con = new SqlConnection(connectionString))
    {
        //
        // Open the SqlConnection.
        //
        con.Open();
        //
        // The following code uses an SqlCommand based on the SqlConnection.
        //
        using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
        using (SqlDataReader reader = command.ExecuteReader())
        {
        while (reader.Read())
        {
            Console.WriteLine("{0} {1} {2}",
            reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
        }
        }
    }
    
06.01.2016 / 17:11