问题
Before reading please note that I've googled this and read a ton of articles on SO and elsewhere, no suggestions have worked as of yet.
I'm randomly getting a timeout error that occurs when logging into my MVC 3 application. It happens well over half of the time - all other times the application logs on just fine. When it doesn't work, it tries for about 10 seconds then errors.
The error:
Exception: "An error occurred while executing the command definition. See the inner exception for details." Inner Exception: {"Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.\r\nTimeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."}
This happens inside my repository class that directly interacts with entity framework.
It seems to happen when logging in and simply pulling a quick check from the database, such as:
return entities.Users.SingleOrDefault(user => user.UserName == userName);
return (entities.Users.SingleOrDefault(u => u.UserId == user.UserId || u.UserName == user.UserName) != null);
Things I've tried:
- SQL Server validation
- Integrated Security (I even gave every possible account full database access)
- Running outside of IIS
- Setting Connect Timeout extremely high (Connect Timeout=50000) in the connection string. (I do not have Default Command Timeout set here)
- Setting the CommandTimeout to 0, 5000, 100000, whatever, on my entity connection: entities.CommandTieout = 100000;
- Setting the CommandTimeout inside every using statement where I use an instance of the repository.
- Flipping SingleOrDefault to FirstOrDefault etc.
- Enabling/Disabling Lazy Loading (Why not?)
If it helps:
- I am using a custom role and membership provider.
- I'm just making calls from my controller inside a using statement (AccountRepository bleh = new AccountRepository()) and the AccountRepository implements IDisposable etc.
- The entity model is in a separate project.
- I'm running the site in IIS. It's setup with the 4.0 integrated app pool.
- All accounts have full database access.
- When the error occurs, it doesn't take no where near as long as I have set in the web config (50000 I think) or for the commandtimeout in the repository.
- It's not doing much on the login, just validating user, getting user role then loading up some small amount data, but the error always occurs when getting the user data on login.
- When I try it outside of debugging it repeats the error four or five times (with custom errors off).
Here is the full exception from the event log:
Exception information:
Exception type: SqlException
Exception message: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
回答1:
You could examine the problem from the database server by setting up a SQL Server Profiler.
You can find lots of info about SQL Profiler by just googling around. Here's a site with a video that might help you get started.
回答2:
Edit: While this did indeed help me it was not the solution. The problem still exists for anyone reading in the future.
Just to let everyone know - I believe that I have found the issue. Through the SQL Profiler, I saw that the account being used to access SQL was in fact the local system account. I then realized that in an attempt to fix an issue prior, I had changed the ASP.NET v4.0 app pool to use the local system account. I went and changed the Identity back to 'ApplicationPoolIdentity', added the IIS APPPOOL\ASP.NET v4.0 user to the database and so far everything has been working great. @DOK - Thank you much for the information on SQL Profiler, it helped tremendously! Thanks everyone else also!
来源:https://stackoverflow.com/questions/9068847/mvc-3-ef-sql-server-strange-connection-timeout-issue