Forcing a query timeout in SQL Server Forcing a query timeout in SQL Server sql-server sql-server

Forcing a query timeout in SQL Server


run this and then try your insert...

select * from yourTable with (holdlock,tablockx)

here, you can lock it for 5 minutes:

BEGIN TRANSACTIONSELECT * FROM yourTable WITH (TABLOCKX, HOLDLOCK)WHERE 0 = 1WAITFOR DELAY '00:05'ROLLBACK TRANSACTION


You can just tell your sql code to wait for a minute before returning:

WaitFor Delay '00:01:00'


On the flip side: If connection is configurable, reduce connection string timeout to 1 second - that will make it easier. Fill the table with oodles of data and have 3 other processes spin in a loop updating chunks of that table with a transaction around the loop. Do not alter actual procedure called by the app (injecting waitfor). That invalidates an integration test.

But really, this is a case study in favor unit testing and dependency injection. Some things are just hard to integration test. Unit test + dependency injection.

  • Real: Code that craps -> Database timeout (hard to reproduce).
  • Refactor: Code that craps -> Repository (does data access only) ->Database
  • Unit test: Code that craps > Mock repository to throw -> null
  • Now you have a failing test for code that craps and can fix it.

This is "dependency" injection. The dev can inject the dependency to the database, substituting something that simulates the behavior of a dependency. Good to do for all database tests. Anyway, with the unit test in place, you know the fix does sort of what it should, but you still need an integration testing. In this case, it may better focus on regression - which means testing it didn't break anything else and the feature still works.

You've already created your patch, so I guess my answer is too late.