System.Data.SqlClient.SqlException (0x80131904): Connection Timeout Expired - c#

I have a Windows Service(c#) that receives data and inserts it into DB using Entity Framework 6. Database is SQL Server 2014 Enterprise Edition. Both (Service and DB) are hosted on same VPS( Windows Server 2008 R2 with 4 GB RAM). The load on the server and service is too low, approx 60 records per minute.
Now intermittently I get the below error in Logs:
Exception:
The underlying provider failed on Open.
Inner Exception:
System.Data.SqlClient.SqlException (0x80131904): Connection Timeout
Expired. The timeout period elapsed during the post-login phase. The
connection could have timed out while waiting for server to complete
the login process and respond; Or it could have timed out while
attempting to create multiple active connections. The duration spent
while attempting to connect to this server was - [Pre-Login]
initialization=0; handshake=7145; [Login] initialization=0;
authentication=0; [Post-Login] complete=7387; --->
System.ComponentModel.Win32Exception (0x80004005): The wait operation
timed out
Any help regarding fixing this shall be greatly appreciated.
EDIT
Connection String:
Data Source=.;Initial Catalog=wheeltrack;UID=wheeltrack;PWD=wheeltrack; MultipleActiveResultSets=true;Max Pool Size=1000;

Related

Can't connect to SQLLocalDB sometimes when PC is offline

We are using SQLLocalDB to connect to a local database. No network access is required - C# Client application is on the same machine as SQLLocalDB. Sometimes I get the following error (only seems to happen on some PCs):
Microsoft.SqlServer.Management.Common.ConnectionFailureException: Failed to connect to server (localdb)\myserverinstancename. ---> System.Data.SqlClient.SqlException: Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=27888; handshake=0; ---> System.ComponentModel.Win32Exception: The wait operation timed out
Oddly, it seems to happen if the PC is offline, and if connected to any sort of internet connection and retried, the issue goes away. However, in most cases the connection works fine even when there is no connection.
The closest I've found to my problem is this: Connection to SQL Server Works Sometimes
But that is discussing full SQL Server, rather than SQLLocalDB, and the solution uses "Sql Server Configuration Manager", which doens't work with SQLLocalDB.
My questions:
Does SQLLocalDb use IPv6/TCP for some reason, even when on the same PC?
Is there a way to change these settings for SQLLocalDB?
Or is there a different cause for this issue?
Edit: With help from some of the comments below I realised that the line that fails is when it calls a database creation script. The initial attempt at connection seems to work, but when I run my creation script it throws a "Failed to connect to server".
/// This line works
var server = new Server("(localdb)\myserverinstancename");
/// this line works 99% of the time, but sometimes "failed to connect to server"
server.ConnectionContext.ExecuteNonQuery(databaseCreationScript);

MySQl Command timeout during read operation from table having 40+ million records

We have a strange situation where we are getting below error when using mysql with EF6.
MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution. --->
MySql.Data.MySqlClient.MySqlException: Fatal error encountered attempting to read the resultset. --->
MySql.Data.MySqlClient.MySqlException: Reading from the stream has failed. --->
System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. --->
System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
If, we take the generated query from EF6 to MySql workbench, it execute without any error in 75 sec.
But, when running in application it throws error within 30 seconds.
The table has huge records, which will take more than 70 sec to return with rows. But thats fine only thing is "it should not throw error due to read timeout failure".
Tried solution:
We have changed the Global as well as mysql settings for read timeout to 99999 but, still it shows the same error.
SET GLOBAL net_read_timeout=999999;
SET net_read_timeout = 999999
You can increase the timeout (default is 30) in either the connection string or the DbContext Database object.
C#
DbContext.Database.CommandTimeout = 999;
Connection string
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
default command timeout=999;

"The wait operation timed out" during SMO SqlBackup - How do I prevent this?

I have a C# WPF application that needs to back up a local database every so often. I do so using the following code:
//Initialise the backup
Microsoft.SqlServer.Management.Smo.Backup backUp = new Microsoft.SqlServer.Management.Smo.Backup();
backUp.BackupSetName = "File location";
backUp.Devices.AddDevice("File location", DeviceType.File);
backUp.Database = "Database name";
backUp.Incremental = false;
backUp.Initialize = true;
backUp.Action = BackupActionType.Database;
backUp.PercentComplete += new PercentCompleteEventHandler(OnBackupUpdate);
//Back up the database
backUp.SqlBackup(Server);
I have been using the application on Windows XP, Windows 7 and Windows 8 for many years and never had an issue with it. However, I recently started using it on Windows 10 and on occasion (about 5% of the time, but that's a pure estimate) the backup begins and after 10 minutes the following error is recorded in my application's error logs.
28/01/2020 12:46:32 BackupDatabase -> Calling BackupDatabaseSMO
28/01/2020 12:56:32 The wait operation timed out
28/01/2020 12:56:32 Win32 Error: 258 -->
28/01/2020 12:56:32 Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
28/01/2020 12:56:32 SQL error number = -2 --> SQL server = (local)\MyServer --> SQL state = 0 --> Error code = -2146232060 --> SQL Error: System.Data.SqlClient.SqlError: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. -->
28/01/2020 12:56:32 An exception occurred while executing a Transact-SQL statement or batch.
28/01/2020 12:56:32 Unknown exception
28/01/2020 12:56:32 Backup failed for Server 'MyPC\MyAccount'.
28/01/2020 12:56:32 Unknown exception
28/01/2020 12:56:32 Failed to backup the database 'LocalWpfDb' on server 'MyPC\MyAccount'. The following message was returned: The wait operation timed out
This has only started happening now I have moved to Windows 10, and it only happens on occasion. Does anyone know of any likely explanations for this, and whether there is a setting I can change on the PC or in the application to stop this from happening?

C# Sql Connection can not connect to server

I'm trying to connect SQL server in my virtual machine with following connection string: "Server=qqvm;Database=a_test;Integrated Security=False;User Id=sa;Password=<pass>;Connect Timeout=0"
Firstly I had 'Network connection' exception with inner 'Access Denied' exception with empty stack trace. I did some digging and enabled 'Named Pipes' in SQL Configuration Manager.
After that I got this exception:
Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=11281; handshake=1;
However, I'm still able to connect via SQL Management Studio to that server remotely. Any ideas?
Finally in my case specifying IP address instead of qqvm name solved the problem. Still don't know why, though. It worked a week ago.

A transport-level error has occurred when sending the request to the server?

I have a scenario in My programmer to check database connection and if database connection available then proceed else try to reconnect it.
I want to fetch DB time using "SELECT GETDATE()" statement. First time when DB connects it runs successfully and when after disconnect and connect DB and trying to fetch DB time it give me below error.
A transport-level error has occurred when sending the request to the server. (Provide: TCP Provider, error: 0 – An existing connection was forcibly closed by the remote host).
Can anyone help me to sort out this Problem?

Categories

Resources