1

I have a .Net application that is creating a new database. The current code is working great in development environment and in many production environments. So I am confident the code is fine.

However, we have a specific instance where the user is getting a timeout while the application is running the following SQL Command:

CREATE DATABASE NameOfDatabase

The code is pretty simple, and as you can see it uses the default timeout period for SQL commands which is 30 seconds:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    string query = "CREATE DATABASE " + databaseName;

    SqlCommand command = new SqlCommand(query, connection);

    connection.Open();
    command.ExecuteNonQuery();
}

Note: our log file shows the error occurs on ExecuteNonQuery which suggests that this is NOT a timeout while opening the connection, and rather during query execution.

The specific .Net error is:

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

With a line from the stack trace to show my reasoning on it being a command timeout (not a connection timeout):

at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

Note: This error information is obtained using a try/catch around the code which was left out of the question for simplicity as it is not relevant to the problem.

Questions

  1. Under any reasonable (or even uncommon) situations, should a CREATE DATABASE query take this long to run and still be successful?

  2. If it shouldn't take that long, what are the common causes for a timeout? (where should I be looking to debug the problem?)

musefan
  • 47,875
  • 21
  • 135
  • 185
  • CREATE DATABASE requires an immediate write to disk ... perhaps there is an issue there – Alex K. Jul 05 '17 at 16:14
  • Make sure that the permissions are correct on that server. – Chuck Jul 05 '17 at 16:15
  • @chuck the user is quite certain permissions are ok, but I can request a double check. Would this cause a timeout though, would we not get a more descriptive error about access or authentication if this was the cause? – musefan Jul 05 '17 at 16:18
  • The best way would be to check the SQL logs and see what is really happening. Maybe you are passing creds and that server does not have mixed mode enabled? – Chuck Jul 05 '17 at 16:28
  • One more think that I thought of, also check the default file location, to make sure that those drives / directories are available. – Chuck Jul 05 '17 at 16:32
  • @chuck thanks I will try and find some sql log info with the user. I believe the user has selected windows authentication, but im almost certain I have seen more specific errors when its that kind of problem... could be mistaken though – musefan Jul 05 '17 at 16:51
  • @alexk i have requested the user check disk space but not response on that yet. I am quite convinced the issue is with sql server not being able to create the database for some reason, just not sure how to identify the explicit cause other than to just checking things as they come to mind – musefan Jul 05 '17 at 16:54
  • 2
    @musefan, good luck. I do think that it is that server / config. You could also see what happens in SSMS, that would be funny if it took 31 seconds to create! – Chuck Jul 05 '17 at 16:59
  • If you login to the SQL server with the .NET program's credentials and run `CREATE DATABASE mydatabase` what happens? Is it working? – Dávid Molnár Jul 06 '17 at 09:58

1 Answers1

0

I do not have enough rep points to comment. To get the specific error wrap command.ExecuteNonQuery(); in a try catch. at least you can get the specific error that is occurring. Also use using with SqlCommand that will dispose that object. Connection State

using (SqlConnection connection = new SqlConnection(connectionString))
{
    string query = "CREATE DATABASE " + databaseName;

    using(SqlCommand command = new SqlCommand(query, connection))
    {
        try
        {
            connection.Open();
            if (connection.State == ConnectionState.Open)
                command.ExecuteNonQuery();
        }
        catch(Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    }               
}

Maybe this TechNet article can help? Troubleshooting: Timeout Expired

Aaron. S
  • 467
  • 4
  • 12
  • I already have the specific error. Its in the question. Fair point about using block but its not relevant for this problem so lets try to keep on topic please – musefan Jul 05 '17 at 16:16
  • I have only experienced this once and the error VS debugger gave me was different than when the catch caught it. which is why I suggested wrapping in try catch. the error reported **may** be more precise. – Aaron. S Jul 05 '17 at 16:19
  • The error message I posted *is* from a try/catch. Its also worth noting I cannot make code changes in order to debug this issue – musefan Jul 05 '17 at 16:55
  • can you check if the connection opened? connection.open doesn't return anything, but you can check the status? the ExecuteNonQuery will timeout if connection didn't open. you suggest a 'specific instance' maybe permission issue with user? – Aaron. S Jul 05 '17 at 17:00
  • Pretty sure connection.open() will throw an error if it isnt successful – musefan Jul 05 '17 at 17:15
  • under certain circumstances it will cause an Exception but you are not getting those Exceptions: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open(v=vs.110).aspx – Aaron. S Jul 05 '17 at 17:23
  • based on the initial question, the answer provided answers correctly your question. if the answer does not actually answer your question please edit your post and clarify the issue. – Aaron. S Sep 06 '17 at 19:14
  • No, you have **not** answered my question. If fact you seem to be purposefully ignoring everything I say that points out why your answer is wrong. In fact you haven't even posted an answer, you have just suggested using a try/catch in which **I have already told you I am using a try/catch** and that is what have given me the error message that **is already in the question** – musefan Sep 07 '17 at 08:19
  • your original question does not include the try/catch you claim to be using. therefore my answer suggests you use it. if you are doing more than what the original question states then include that so anyone answering your question has all of the facts. we cant read your mind and you cannot assume we know everything you have done either. revise your original question with things you have tried so we have all of the facts – Aaron. S Sep 07 '17 at 16:58
  • You don't need to read my mind, just read the words where I specifically tell you in the comment. Also you don't provide an answer, you have provided a basic suggestion which should have been a comment - you even say that yourself as your first sentence – musefan Sep 11 '17 at 08:50
  • More importantly your suggestion is wrong, wrapping something in a try/catch does not change the error that gets thrown, only changes how it is handled. The exact error was included in my question from the start, it doesn't matter if that error came from am try/catch or the debugger, or whatever – musefan Sep 11 '17 at 08:55
  • the databaseName variable you are using, is it parameterized? https://stackoverflow.com/questions/3523989/how-to-use-sqlcommand-to-create-database-with-parameterized-db-name – Aaron. S Sep 11 '17 at 15:54
  • is this a linked server? could this be a permission issue? these two combined, at least in my experiences, would cause this – Aaron. S Sep 11 '17 at 15:54
  • `databaseName` is not parameterized. Dont know about linked server, and I have been assured it's not a permissions issue although I am not sure if that is correct. I don't have access to the database myself to check. At this point I am not going to be able to test this will the original user anymore. I will have to wait until I can replicate the issue myself before I can do any further testing – musefan Sep 12 '17 at 08:14