4

Let's say we are executing lots of different sql command, and the SqlCommand.CommandTimeout was leave with default value 30 seconds.

And let's just assume some of those sql command are just long query and we might get the timeout exception.

Correct me if I'm wrong, this exception was just cause .Net do not want to wait anymore, but if we are using connection pool, this connection might be remain open, so will that sql statement might still running on the SQL server side? Or there are some hidden communication between those system to suddenly stop it no matter we are using connection pool or not?

Just want to know what's the mechanism and would it effect the SQL server performance. I mean if the query is really long such as take 10 mins to run if it's still running it might just slow down the server unnecessary as no one can get the result.

UPDATE

So here I'm asking about the connection pool specifically, it's definitely that the code will close the connection with exception handling, or we can just assume that are using code which is a pattern preferred named by @dash here. The problem is if I call the Close() or Dispose() method on that SqlConnection object, it is returned to the connection pool, it's not physically close it.

I'm asking when it's returned to pool, would that long query still running on SQL Server side. And if possible, how to avoid that.

UPDATE again

Thanks for @dash mentioning that about database transaction, yes a rollback will make it wait and we are not closing the connection and return it to the pool yet. So what if it's just a long select query or an update but just one individual update without any database transaction involved? And specifically I want to know is there a way that we can tell SQL Server that I do not need the result now please stop running it?

2
  • 1
    No; there is no way - you can KILL the running process on SQL Server, but you really just need to let SQL Server manage what it's doing. As with everything, if you choose to run a query that degrades the performance of the server, then that's your choice :-) Commented Mar 20, 2012 at 10:23
  • You can kill the running process on SQL Server by using the SQL KILL command. KILL SPID and to get the SPID you could use SELECT @@SPID or get all of the current processes from one of the sys tables. Commented Mar 28, 2012 at 14:40

3 Answers 3

5

It all depends on how you are executing your queries really;

Imagine the following query:

SqlConnection myConnection = new SqlConnection("connection_string");

SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandTimeout = some_long_time;
myCommand.CommandText = "database_killing_procedure_lol";

myConnection.Open() //Connection's now open

myCommand.ExecuteNonQuery();

Two things will happen; one is this method will queue until the command.ExecuteNonQuery() finishes. The second is that we will also tie up a connection from the connection pool for the duration of the method.

What happens if we timeout? Well, an exception is thrown - a SqlException with a Number property = -2. However, remember, in the code above, there is no exception management so all that will happen is the objects will go out of scope and we'll need to wait for them to be disposed. In particular, our connection wont be reusable until this happens.

It's one of the reasons why the following pattern is preferred:

using(SqlConnection myConnection = new SqlConnection("connection_string"))
{
    using(SqlCommand myCommand = new SqlCommand())
    {

        SqlCommand myCommand = new SqlCommand();
        myCommand.Connection = myConnection;
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.CommandTimeout = some_long_time;
        myCommand.CommandText = "database_killing_procedure_lol";

        myConnection.Open() //Connection's now open

        myCommand.ExecuteNonQuery();    

    }

}

It means that, as soon as the query is finished, either naturally (it runs to completion) or through an exception (timeout or otherwise), the resources are given back immediately.

In your specific issue, having a large number of queries that take a long time to execute is bad for many reasons. In a web application, you potentially have many users contending for a limited number of resources; memory, database connections, cpu time and so on. Therefore, tying up any of these with expensive operations will reduce the responsiveness and performance of your web application, or limit the number of users you can serve simultaneously. Futher, if the database operation is expensive, you can tie up your database, too, further limiting perofrmance.

It's always worth attempting to bring the execution time for database queries down for this reason alone. If you can't, then you will have to be careful about how many of these types of queries you can run simultaneously.

EDIT:

So you are actually interested in what's happening on the SQL Server side... the answer is... it depends! The CommandTimeout is actually a client event - what you are saying is that if the query takes longer than n seconds, then I don't want to wait any more. SQL Server gets told that this is the case, but it still has to deal with what it's currently doing, so it can actually take some time before SQL Server finishes the query. It will attempt to prioritise this, but that's about it.

This is especially true with transactions; if you are running a query wrapped in a transaction, and you roll that back as part of your exception management, then you have to wait until the rollback is complete.

It's also very common to see people panic and start issuing KILL commands against the SQL Process id that the query is running under. This is often a mistake if the command is running a transaction, but is often okay for long running selects.

SQL Server has to manage it's state such that it remains consistent. The fact that the client is no longer listening means you have wasted work but SQL Server still has to clean up after itself.

So yes, the ASP.Net side of things will be all fine as it doesn't care, but SQL Server still has to finish the work it began, or reach a point where it can abandon that work safely, or rollback any changes in any transactions that were opened.

This obviously could have a performance impact on the database server depending on the query!

Even a long running SELECT or UPDATE or INSERT outside of a transaction has to finish. SQL Server will try and abandon it as soon as it can, but only if it's safe to do so. Obviously, for UPDATES and INSERT's especially, it has to reach a point where the database is still consistent. For SELECT's it will attempt to end as soon as it is able to.

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

5 Comments

I'm sorry that I'm not clear about my question, I'm asking about the connection pool but not better practicing with 'using' keyword, please check my updated question.
It's the same thing. If you don't release you connections as soon as you are finished with them, then they wont be available for reuse in the connection pool until they are disposed. The example highlights how you can unintentionally keep hold of a connection for longer than you expect. The last part talks about performance issues you might have from executing many long running queries.
Hey I just update my question, please check, I'm really asking a different question which is I do closed the connection and it returns to connection pool, would it still running on sql server side. I'm not asking how .Net handle connection pool. thanks
The answer is often yes - it will still continue running. I've added a section at the end that discusses this.
Thank you dash, although can't find a formal documentation I still would like to accept your answer
0

Thanks for @dash mentioning that about database transaction, yes a rollback will make it wait and we are not closing the connection and return it to the pool yet. So what if it's just a long select query or an update but just one individual update without any database transaction involved? And specifically I want to know is there a way that we can tell SQL Server that I do not need the result now please stop running it?

I think This link will answer need Link1 Link2

1 Comment

Note the documentation - "Tries to cancel the Execution of a SqlCommand" - the operative word is "tries" - it's not guaranteed, I'm afraid.
0

The SqlConnection.ClearPool() method may be wait you're looking for. The following post touches on this.

How to force a SqlConnection to physically close, while using connection pooling?

Comments

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.