TransactionScope 和超时问题
我们知道TransactionScope类可以使用用户定义的超时值。但是从 using {} 块退出时会引发超时异常。如何在超时值过去后立即抛出此超时异常?
We know that TransactionScope class can use user-defined timeout value. But timeout exception is thrown while exiting from the using {} block. How to throw this timeoutexception immediately after elapsed timeout value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不可能的。
TransactionScope 只是存储您启动事务的时间,然后在提交事务时检查该时间。
它无法在任意点抛出异常。
一般来说,可以在(托管代码)执行中的任何点抛出的唯一异常是 ThreadAbortException。
因此,如果您确实愿意,您可以创建一个单独的线程,在超时期间休眠,然后中止原始线程。
然而,这是一个可怕的想法。
This is not possible.
The TransactionScope simply stores the time that you started the transaction, then checks that time when committing the transaction.
It has no way to throw an exception at any arbitrary point.
In general, the only exception that can be thrown at any point in execution (of managed code) is
ThreadAbortException
.Therefore, if you really wanted to, you could make a separate thread that sleeps for the duration of the timeout, then aborts your original thread.
However, that's a horrible idea.