当我尝试使用 sp_setapprole 在 SqlConnection 上设置应用程序角色时我有时会在 Windows 事件日志中收到以下错误...
The connection has been dropped because the principal that opened it subsequently assumed a new security context, and then tried to reset the connection under its impersonated security context. This scenario is not supported. See "Impersonation Overview" in Books Online.)
... 并且在我的应用程序中抛出了一个匹配的异常。
这些是连接池,曾经有一段时间连接池与应用程序角色不兼容 - 实际上微软的旧建议是 disable connection pooling (!!) 但随着 sp_unsetapprole 的引入现在(理论上)可以在将连接返回到池之前清理连接。
我相信这些错误发生在(由于未知原因)sp_unsetapprole 在连接关闭并返回到连接池之前未在连接上运行时。当此连接从池中返回时,sp_approle 注定会失败。
我可以捕获并处理此异常,但我更愿意检测即将发生的故障并完全避免异常(以及事件日志中的消息)。
是否可以在不引起异常的情况下检测到问题?
欢迎提出想法或建议。
最佳答案
这是合乎逻辑的,并没有太多使用 sp_setapprole 的经验,但是在调用之前不可能检查安全上下文吗?或者先检查安全权限和上下文?
关于c# - 检测不可用的池化 SqlConnections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/553331/