Closing a SQLConnection in catch statement of try/catch block - c#

sc is a SQL connection passed in from the calling function
I'm using one SQL connection throughout my entire program and having each subfunction open and close it as needed. (I read this was fine, because the SQLConnection class has it's own connection pooling functionality)
string q = "this would be a query";
using (SqlCommand cmd = new SqlCommand(q, sc))
{
cmd.Parameters.Add("#ID", SqlDbType.Int);
cmd.Parameters["#ID"].Value = (an id that is passed in);
try
{
sc.Open();
using (var reader = cmd.ExecuteReader())
{
if(reader.read())
{ logic that might throw exception }
else
{ logic that might throw exception }
}
sc.Close();
}
catch (Exception e)
{
alert user, program does not halt but current function returns
}
}
So the connection opens and the command attempts to execute and some logic is performed that might throw an exception. (I'm not expecting it to) This function is part of a larger program that is processing a large dataset. If it fails DUE TO AN EXCEPTION it needs to note it, but it also needs to attempt to keep running.
Other parts of the program are sharing this connection and opening and closing it as they need it and I've used this basic template for all queries in the program.
It just occurred to me that if an exception occurred, the connection might not be closed. Am I correct in thinking that? My original concern when writing this is that the sc.Open() would be the thing that would throw an exception, but as I've used this in multiple places I've realized that this was short sighted because the connection could open and then any number of steps between the close could throw an exception.
Do I need additional logic here to make sure this connection is closed and the program can attempt to continue running?
If my understanding is correct that the connection needs to be closed in the event of an exception between the open and close I think I have 2 options :
try/catch between the open and close
sc.Close() in the catch block (Bonus concern : If the sc.Open() throws an exception and catch block tries to sc.Close() will an additional exception be thrown?)

You do not need to worry about closing the connection. The connection is closed automatically when it disposed by the using statement within which the connection is created.
In the event of an exception, the connection will still be disposed (and thus closed) because that's how a using statement is designed to work.

You have the State property of the SqlConnection that you can use to determine what state the last network operation left the connection in. This property is described here: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.state(v=vs.110).aspx
I would recommend reacting to exceptions in the SqlConnection (such as Open) and in the SqlCommand separately, because, in my experience, you will usually want to take different actions. Even with the overall approach you want to take,
if you can't open your connection, there is no point in preparing the query,
and you likely want to say something different about inability to open the connection than you would if the query failed. I would move the sc.Open() before the creation of the SqlCommand and try/catch around that.

Clarification on the above answer by #roryap - there is no need for sc.Close() if your connection is created in a using statement but from your question I guess it's not. In which case you should probably add a finally section to the try catch block and close the connection in there.

Its better that you close the connection in finally block
finally
{
sc.Close();
}
After try block is completed, the finally block will work.
In a case of an exception, finally block also works.
So the connection will closed in any case.

Related

Closing a null sql connection

I have a question about closing an opened connection to a database in C#. Let's say we abandon the "using" method and use a try/catch/finally block to open and close the connection.
try
{
connection = new SqlConnection();
connection.Open();
}
catch (Exception ex)
{
// Do whatever you need with exception
}
finally
{
1.connection.Dispose();
2.if (connection != null)
{
connection.Dispose();
}
}
My question here is what exactly happens if only 1) in this code segment occurs. The connection should always be checked for null before disposed(closed), but what if it isn't, and we try to close a connection object that's null? (I'm interested in what happens to the opened connection, can this lead to a leak)
In the question, if connection.Dispose() is called on a null connection without the check, you cause a new exception that has not been handled, and everything that goes with that. Additionally, if this was done because the connection variable was set to null before the finally block without ensuring the connection was closed, it is possible to leak that open connection ... though as a practical matter it's rare to see code in the wild that knows to use a finally but also wants to set the connection to null.
Granting the lack of using (which is suspect, but whatever), I prefer this pattern:
finally
{
if (connection is IDisposable) connection.Dispose();
}
This still protects against null values* in the connection object, and is a closer mimic for what the using pattern was already doing behind the scenes.
Note, however, you're still missing one important feature of a using block, which is protection against something assigning null to your variable before the block closes. using blocks also protect your connection in a separate (hidden) variable, so you're sure you still have a valid reference at the end of the block (this is new in C# 8).
*(For completeness, the author of that linked tweet is the lead on the C# compiler team, so he should know, and he's not being ironic).

What effects does it have to opena DB conn outside a "using" block?

I tried to search for this on SO but couldn't find it. Maybe I didn't search properly and it is a duplicate of something.
But I would like to ask a question: what is the difference between opening a DB connection inside a using(...) block and outside.
For clarify what I mean, look at the code snippets below.
Snippet where we open a DB connection outside "using" block:
if (_dbConn.State != ConnectionState.Open)
_dbConn.Open();
using (var oraclePackage = new OraclePackage())
{ // some DB function here... }
Snippet in which a DB connection is opened inside a "using" block:
using (var oraclePackage = new OraclePackage())
{
if (_dbConn.State != ConnectionState.Open)
_dbConn.Open();
// some DB functions here
}
Would the block where I DO NOT open a connection inside "using" still close it in case of an exception or would it be left open?
Would the block where I DO NOT open a connection inside "using" still close it in case of an exception or would it be left open?
In both of those examples, the connection would still be open, until perhaps a finaliser ran, if one ever did. Neither has a using that is "using" dbConn.
The code:
using(someObject)
{
// More code here.
}
Is equivalent to:
try
{
// More code here.
}
finally
{
if(someObject != null)
((IDisposable)someObject).Dispose();
}
And the code:
using(var someObject = new SomeClass())
{
// More code here.
}
Is equivalent to:
var someObject = new SomeClass();
try
{
// More code here.
}
finally
{
if(someObject != null)
((IDisposable)someObject).Dispose();
}
(Null checks might be optimised out in cases where the object clearly cannot be set to null at any point).
Your code does this with oraclePackage so oraclePackage will be guaranteed to have Dispose() called on it, whether the block of code is left due to an exception or not. You code does not do this with dbConn.
As such, if you don't explicitly call dbConn.Dispose() or dbConn.Close() (which dbConn.Dispose() almost certainly calls into) the connection will not be closed.
If there are no remaining GC roots that can access dbConn then it's likely that eventually the internal object that represents the actual open connection will be finalised, which will lead to the connection being closed, but this is not guaranteed to happen or to happen in a timely manner, and it is quite likely not to work as well with the internal caching mechanism most IDbConnection implementations use.
As you are not applying using on connection object the connection will remain open if exception occurs...so you need finally block which close your connect even there is exception.
so your code will be , as you are not applying using on connection object
try
{
using (var oraclePackage = new OraclePackage())
{
if (_dbConn.State != ConnectionState.Open)
_dbConn.Open();
// some DB functions here
}
}
finally
{ _dbConn.Close(); }
Using statement does not handle any exceptions. It is responsible for calling IDisposable on current object (DB connection).
This means that no matter if you open connection inside or outside the using block, at the end of the block the connection will be disposed

Which is the better way to "handle" an exception I want to let bubble up but execute clean up code

I am working on a new project and have a section of code where I don't want to actually handle exceptions but make sure any DB connections are closed. I am wondering which will be the best way to handle the issue. I see 2 ways but am not sure which will be clearer:
SqlConnection con = new SqlConnection(connectionstring);
try
{
con.open()
//DoStuff
}
catch(Exception)
{
throw;
}
finally
{
con.close();
con.dispose();
}
OR
try
{
con.open()
//DoStuff
}
finally
{
con.close();
con.dispose();
}
Either way I pass the exception up to the calling code to be handled but still clean up the connections.
You can use using statement to enclose your connection. It would translate into try-finally block, like in your second code example.
Since you are not doing anything with your exception (like logging) you can leave out the catch block.
You can use using like:
using (SqlConnection con = new SqlConnection(connectionstring))
{
}
This will ensure your connection is disposed at the end of using block, even in case of an exception. You can only use using statement with those objects which implements IDisposable interface.
If you are going to deal with objects which doesn't implement IDisposable then your second code snippet (With try-finally) is good enough, since you want the exception to bubble up.

C#/ SQL Server error 'There is already an open DataReader associated with this Command which must be closed first.'

So, I'm getting the error as per my title line. Seems pretty self- explanatory, but my understanding is that objects within the "using" block are disposed of? This error appeared after another minor bug interrupted code execution, so perhaps I'm stuck with an open reader that I need to close or shut down? Any help would be appreciated?
public override long GetStatsBenchmark(String TableName)
{
using (SqlCommand cmd = new SqlCommand("sprocReturnDataPointBenchmark", this.sqlConnection))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter outputParameter = new SqlParameter
{
ParameterName = "#benchmark",
Direction = System.Data.ParameterDirection.Output,
SqlDbType = System.Data.SqlDbType.BigInt,
};
cmd.Parameters.Add(outputParameter);
SqlParameter inputParameter = new SqlParameter
{
ParameterName = "#tblName",
Direction = System.Data.ParameterDirection.Input,
SqlDbType = System.Data.SqlDbType.NVarChar,
Value = TableName
};
cmd.Parameters.Add(inputParameter);
cmd.ExecuteNonQuery();
return (long)outputParameter.Value;
}
}
I do not think that the code you have shown is the cause of the problem. While it is good practice to dispose of SqlCommand (IDbCommand) objects, in practice I have not found it to be necessary. What is necessary is to dispose of SqlDataReader (IDataReader) objects after you have finished using them. As the error message suggests, look for a usage of an SqlDataReader object in your code that is not being disposed. The exception may be thrown from the code you are displaying, but I would suspect that the cause is because of an SqlDataReader associated with the same SqlConnection used earlier in the program execution that has not been disposed.
An issue with the using clause is that it does not provide a way to process exceptions in the implicit try/finally block that the compiler generates for you.
You can
1) wrap your using clause with a try/catch, or
2) manually code a try/catch/finally instead of using, calling Dispose in the finally block and adding exception handling in a catch block.
There are slight drawbacks to either technique, but either will work
You can add MARS (multiple active result sets) to your connection string, to allow multiple recordset (or similar) open at the same time.
Add
MultipleActiveResultSets=true;
or
MARS Connection=True;
to your connection string (http://www.connectionstrings.com/sql-server-2008) and this will allow multiple handles, but if the previous operation has been "interrupted" in an exception, try avoid using "USING" or use a try/catch inside it.

True coding convention about usings/try/catches

Let's assume i got this code:
internal static bool WriteTransaction(string command)
{
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
try
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(command, conn))
cmd.ExecuteNonQuery();
}
catch { return false; }
}
return true;
}
Well, i have placed conn's using outside the try/catch clause because SqlConnection's constructor will not throw any exception (as it says). Therefore, conn.Open() is in the clause as it might throw some exceptions.
Now, is that right coding approach? Look: SqlCommand's constructor does not throw exceptinos either, but for the code reduction i've placed it along with cmd.ExecuteNonQuery() both inside the try/catch.
Or,
maybe this one should be there instead?
internal static bool WriteTransaction(string command)
{
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
try { conn.Open(); }
catch { return false; }
using (SqlCommand cmd = new SqlCommand(command, conn))
try { cmd.ExecuteNonQuery(); }
catch { return false; }
}
return true;
}
(sorry for my english)
Unless you can handle the exception in some meaningful way, do not catch it. Rather, let it propagate up the call-stack.
For instance, under what conditions can a SqlCommand ExecuteNonQuery() throw exceptions? Some possibilities are sql query that is improperly formed, cannot be executed or you've lost connection to the database server. You wouldn't want to handle these all the same way, right?
One exception you should consider handling is the SQLException deadlock (erro number 1205).
As was pointed out in a comment, at the very minimum you should be logging exceptions.
[BTW, WriteTransaction() is probably a poor name for that method, given the code you have shown.]
Your first code sample, with a single try-catch block, is equivalent to the second. The first one is, however, easier to read and shorter.
It's worth bearing in mind that the usual C# coding approach is not to catch exceptions except at the very top layer of your code.
A well-written reference on this is here: http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx.
In your case this will simplify your code: instead of returning a bool to indicate that the method succeeded or failed, then testing for this, you "assume for the best" by making the method void and merely handle unexpected exceptions at a top level exception handler.
Exception handling for writing to databases can be considered a slight exception to this general rule: but my personal approach would be to trap specifically for concurrency issues and if that happens retry a few times.
I think your first solution is better because if you get an error establishing connection is not useful trying to execute a command on that connection.
So with first solution if you get an error you go directly do catch block and then dispose objects, while with the second you go through two exceptions, overloading memory useless.
with good catch clause (exceptions logging) you don't realy need 2x try block, 1 is enough
...
your second approach is wrong - there's no need to try catch openning connection - becouse even if you catch this you are done anyway - you can't execute query. In your catch block you could try to "repair" that problem, but what would it give you - while loops in catch ?
Your first approach is the preferable one, for a very simple reason: The two versions of the method are identical in terms of behavior, but the first one needs less code.
Sometimes it's as simple as counting lines of code ;-)...
HTH!
P.S.
I wouldn't have a try/catch statement in this method at all, but I would place it on a higher level, where this internal method is invoked.
Given your method 'WriteTransaction' is swallowing all exceptions and returning the success of the transaction, or not, then I would use one try/catch block around the entire method.
internal static bool WriteTransaction(string command) {
try {
using (SqlConnection conn = new SqlConnection(SqlConnectionString)) {
conn.Open();
using (SqlCommand cmd = new SqlCommand(command, conn)) {
cmd.ExecuteNonQuery();
}
}
}
catch {
return false;
}
return true;
}
Now you should ask yourself if catching all exceptions and returning true/false is the right thing to do. When in production how are you going to diagnose and fix errors if you haven't at least logged the exception?

Categories