<p>Every now and then in a high volume .NET application, you might see this exception when you try to execute a query:</p>

<blockquote>
  <p>System.Data.SqlClient.SqlException:  A transport-level error has
  occurred when sending the request to the server.</p>
</blockquote>

<p>According to my research, this is something that "just happens" and not much can be done to prevent it.  It does not happen as a result of a bad query, and generally cannot be duplicated.  It just crops up maybe once every few days in a busy OLTP system when the TCP connection to the database goes bad for some reason.</p>

<p>I am forced to detect this error by parsing the exception message, and then retrying the entire operation from scratch, to include using a new connection.  None of that is pretty.</p>

<p>Anybody have any alternate solutions?</p>

[{'answer_id': 16636, 'author': 'Kevin Goff', 'author_id': 1940, 'author_profile': 'https://Stackoverflow.com/users/1940', 'pm_score': 0, 'selected': False, 'text': '<p>You should also check hardware connectivity to the database.</p>\n\n<p>Perhaps this thread will be helpful:\n<a href="http://channel9.msdn.com/forums/TechOff/234271-Conenction-forcibly-closed-SQL-2005/" rel="nofollow noreferrer">http://channel9.msdn.com/forums/TechOff/234271-Conenction-forcibly-closed-SQL-2005/</a></p>\n'}, {'answer_id': 156287, 'author': 'Rinat Abdullin', 'author_id': 47366, 'author_profile': 'https://Stackoverflow.com/users/47366', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m using reliability layer around my DB commands (abstracted away in the repository interfaece). Basically that\'s just code that intercepts any expected exception (DbException and also InvalidOperationException, that happens to get thrown on connectivity issues), logs it, captures statistics and retries everything again.</p>\n\n<p>With that reliability layer present, the service has been able to survive stress-testing gracefully (constant dead-locks, network failures etc). Production is far less hostile than that.</p>\n\n<p>PS: <a href="http://rabdullin.com/how-to-create-configurable-reliability-layer-with-photonnet/" rel="nofollow noreferrer">There is more on that here</a> (along with a simple way to define reliability with the interception DSL)</p>\n'}, {'answer_id': 156388, 'author': 'Daniel Fortunov', 'author_id': 5975, 'author_profile': 'https://Stackoverflow.com/users/5975', 'pm_score': 2, 'selected': False, 'text': '<p>To answer your original question:</p>\n\n<p>A more elegant way to detect this particular error, without parsing the error message, is to inspect the <code>Number</code> property of the <code>SqlException</code>.</p>\n\n<p>(This actually returns the error number from the first <code>SqlError</code> in the <code>Errors</code> collection, but in your case the transport error should be the only one in the collection.)</p>\n'}, {'answer_id': 156401, 'author': 'Jesper Blad Jensen', 'author_id': 11559, 'author_profile': 'https://Stackoverflow.com/users/11559', 'pm_score': 0, 'selected': False, 'text': "<p>I had the same problem. I asked my network geek friends, and all said what people have replied here: Its the connection between the computer and the database server. In my case it was my Internet Service Provider, or there router that was the problem. After a Router update, the problem went away. But do you have any other drop-outs of internet connection from you're computer or server? I had...</p>\n"}, {'answer_id': 207714, 'author': 'Tim Farley', 'author_id': 4425, 'author_profile': 'https://Stackoverflow.com/users/4425', 'pm_score': 3, 'selected': False, 'text': '<p>I posted <a href="https://stackoverflow.com/questions/154897/what-do-you-do-if-you-cannot-resolve-a-bug#155077">an answer on another question</a> on another topic that might have some use here.  That answer involved SMB connections, not SQL.  However it was identical in that it involved a low-level transport error.</p>\n\n<p>What we found was that in a heavy load situation, it was fairly easy for the remote server to time out connections <em>at the TCP layer</em> simply because the server was busy.  Part of the reason was the defaults for how many times TCP will retransmit data on Windows weren\'t appropriate for our situation.</p>\n\n<p>Take a look at the <a href="http://support.microsoft.com/kb/314053" rel="nofollow noreferrer">registry settings for tuning TCP/IP</a> on Windows.  In particular you want to look at <strong>TcpMaxDataRetransmissions</strong> and maybe <strong>TcpMaxConnectRetransmissions</strong>.  These default to 5 and 2 respectively, try upping them a little bit on the client system and duplicate the load situation.</p>\n\n<p>Don\'t go crazy!  TCP doubles the timeout with each successive retransmission, so the timeout behavior for bad connections can go exponential on you if you increase these too much.  As I recall upping <strong>TcpMaxDataRetransmissions</strong> to 6 or 7 solved our problem in the vast majority of cases.</p>\n'}, {'answer_id': 252741, 'author': 'Dale Wright', 'author_id': 11523, 'author_profile': 'https://Stackoverflow.com/users/11523', 'pm_score': 1, 'selected': False, 'text': "<p>I have seen this happen in my own environment a number of times.  The client application in this case is installed on many machines.  Some of those machines happen to be laptops people were leaving the application open disconnecting it and then plugging it back in and attempting to use it.  This will then cause the error you have mentioned.</p>\n\n<p>My first point would be to look at the network and ensure that servers aren't on DHCP and renewing IP Addresses causing this error.  If that isn't the case then you have to start trawlling through your event logs looking for other network related.</p>\n\n<p>Unfortunately it is as stated above a network error.  The main thing you can do is just monitor the connections using a tool like netmon and work back from there.</p>\n\n<p>Good Luck.</p>\n"}, {'answer_id': 2160485, 'author': 'Magnus Lindhe', 'author_id': 966, 'author_profile': 'https://Stackoverflow.com/users/966', 'pm_score': 2, 'selected': False, 'text': '<p>This <a href="http://blogs.msdn.com/spike/archive/2009/04/16/a-transport-level-error-has-occurred-when-sending-the-request-to-the-server-provider-tcp-provider-error-0-an-existing-connection-was-forcibly-closed-by-the-remote-host.aspx" rel="nofollow noreferrer">blog post</a> by <a href="http://blogs.msdn.com/spike/default.aspx" rel="nofollow noreferrer">Michael Aspengren</a> explains the error message "A transport-level error has occurred when sending the request to the server."</p>\n'}, {'answer_id': 3319576, 'author': 'Martin', 'author_id': 338547, 'author_profile': 'https://Stackoverflow.com/users/338547', 'pm_score': 1, 'selected': False, 'text': '<p>I had the same problem albeit it was with service requests to a SQL DB.</p>\n\n<p>This is what I had in my service error log:</p>\n\n<hr>\n\n<p>System.Data.SqlClient.SqlException: A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)</p>\n\n<hr>\n\n<p>I have a C# test suite that tests a service. The service and DB were both on external servers so I thought that might be the issue. So I deployed the service and DB locally to no avail. The issue continued. The test suite isn\'t even a hard pressing performance test at all, so I had no idea what was happening. The same test was failing each time, but when I disabled that test, another one would fail continuously. </p>\n\n<p>I tried other methods suggested on the Internet that didn\'t work either:</p>\n\n<ul>\n<li>Increase the registry values of <strong>TcpMaxDataRetransmissions</strong> and <strong>TcpMaxConnectRetransmissions</strong>.</li>\n<li>Disable the "Shared Memory" option within SQL Server Configuration Manager under "Client Protocols" and sort TCP/IP to 1st in the list.</li>\n<li>This might occur when you are testing scalability with a large number of client connection attempts. To resolve this issue, use the regedit.exe utility to add a new DWORD value named SynAttackProtect to the registry key HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\ with value data of 00000000.</li>\n</ul>\n\n<p>My last resort was to use the old age saying "Try and try again". So I have nested try-catch statements to ensure that if the TCP/IP connection is lost in the lower communications protocol that it does\'t just give up there but tries again. This is now working for me, however it\'s not a very elegant solution.</p>\n'}, {'answer_id': 3319628, 'author': 'Jon Black', 'author_id': 298016, 'author_profile': 'https://Stackoverflow.com/users/298016', 'pm_score': 1, 'selected': False, 'text': '<p>use Enterprise Services with transactional components</p>\n'}, {'answer_id': 22389658, 'author': 'Phillip Deneka', 'author_id': 1721229, 'author_profile': 'https://Stackoverflow.com/users/1721229', 'pm_score': 0, 'selected': False, 'text': "<p>I experienced the transport error this morning in SSMS while connected to SQL 2008 R2 Express.</p>\n\n<p>I was trying to import a CSV with \\r\\n.  I coded my row terminator for 0x0d0x0a.  When I changed it to 0x0a, the error stopped.  I can change it back and forth and watch it happen/not happen.</p>\n\n<pre><code> BULK INSERT #t1 FROM 'C:\\123\\Import123.csv' WITH \n      ( FIRSTROW = 1, FIELDTERMINATOR = ',', ROWTERMINATOR = '0x0d0x0a' )\n</code></pre>\n\n<p>I suspect I am not writing my row terminator correctly because SQL parses one character at a time right while I'm trying to pass two characters.</p>\n\n<p>Anyhow, this error is 4 years old now, but it may provide a bit of information for the next user.</p>\n"}, {'answer_id': 34070807, 'author': 'J. J.', 'author_id': 5635855, 'author_profile': 'https://Stackoverflow.com/users/5635855', 'pm_score': 0, 'selected': False, 'text': '<p>I just wanted to post a fix here that worked for our company on new software we\'ve installed. We were getting the following error since day 1 on the client log file: Server was unable to process request. ---> A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The semaphore timeout period has expired.) ---> The semaphore timeout period has expired.</p>\n\n<p>What completely fixed the problem was to set up a link aggregate (LAG) on our switch. Our Dell FX1 server has redundant fiber lines coming out of the back of it. We did not realize that the switch they\'re plugged into needed to have a LAG configured on those two ports. See details here: <a href="https://docs.meraki.com/display/MS/Switch+Ports#SwitchPorts-LinkAggregation" rel="nofollow">https://docs.meraki.com/display/MS/Switch+Ports#SwitchPorts-LinkAggregation</a></p>\n'}]
['https://Stackoverflow.com/questions/16610', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1219/']