如何导致数据库超时异常?
我需要测试一些处理 MS-SQL 数据库调用超时异常的 ASP.NET 代码。
我可以减少连接上的 CommandTimeout 参数,但这可能会在到达我的代码之前导致错误。
有没有一种简单的方法可以使数据库“按提示”超时?
I need to test some ASP.NET code that handles timeout exceptions from a call to a MS-SQL database.
I'm able to reduce my CommandTimeout parameter on the connection, however this might cause errors before reaching my code.
Is there a simple way to cause the database to timeout 'on cue'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
先生,您正在寻找的是 WAITFOR 命令,队列中的数据库超时每当你愿意的时候。
What you are looking for sir is the WAITFOR command, database timeouts on queue whenever you feel like it.
在 Management Studio SQL 窗口中开始事务,更新表,但不提交它:
这将锁定任何尝试读取该表的网站。
Begin a transaction in a management studio SQL window, update the table, and don't commit it:
This will lock any website attempt to read that table.
难道你不能在你想要测试的任何 try 块中抛出你想要的异常吗?
Couldn't you just throw the exception that you want in whatever try block you want to test?
编写一个在每次迭代中循环执行一些复杂任务的过程......如果这还不够,可以通过递归调用存储过程进一步扩展它!
Write a procedure that loops for ever doing some complex task with each iteration...and if that is not enough further expand it by calling the stored procedure recursively!
我通过拔掉以太网线来便宜这个,哈哈......
Ive cheaped this by unplugging my ethernet cable lol...
开始测试,然后关闭数据库服务器(sc stop“MSSQL$SQLEXPRESS”或其他内容)。
Begin your test, then turn off your database server (sc stop "MSSQL$SQLEXPRESS" or something).
您可以考虑用 mock 替换数据库连接,这可以简化您的测试。
在这种情况下,模拟总是会发出超时信号。
You might consider replacing the DB connection with a mock, that could simplify your testing.
In this case, the mock would simply always signal a timeout.
写下执行时间超过 30 秒的过程或查询,并
从 ASP.NET 代码中调用它。
write down the procedure or Query which take more than 30 seconds to execute and
call this from asp.net code.