Matt Murrell
Matt Murrell

Reputation: 2351

Any way to set small timeout (milliseconds) in SQL Server (using ADO.NET)?

The question is in the title... Any way I set a SqlCommand to timeout in 100ms? The documentation says SqlCommand / ADO supports timeouts in full seconds only.

Basically, I have a very small insert that should take <30ms. Sometimes SQL decides to be slow and my insert is blocked and can take 1-2 seconds. The data inserted, however, is not mission critical and I would rather it fail than wait and block. Can I set a very small timeout using ADO.NET? Or should I just leave the Sql Insert as a 1sec timeout and fire-and-forget using the ASYNC methods?

Thanks in advance.

Upvotes: 2

Views: 804

Answers (1)

Dan Guzman
Dan Guzman

Reputation: 46203

If you want to fail the insert if the lock can't be acquired within a short period of time, set a lock timeout before executing the insert. This will cause a timeout on the server side instead of client side.

SET LOCK_TIMEOUT 100;

Upvotes: 2

Related Questions