简体   繁体   English

解释SQL异常

[英]Interpreting SQL Exceptions

In my application, I have a data access layer that abstracts the other layers from communication to the database. 在我的应用程序中,我有一个数据访问层,该层将从通信到数据库的其他各层抽象化。

Whenever an exception is caught, I log the exception stack trace to help pinpoint the issue, and then handle the exception. 每当捕获到异常时,我都会记录异常堆栈跟踪以帮助查明问题,然后处理该异常。 This is all fine and dandy, except when it comes to connection problems. 除涉及连接问题外,其他所有操作都很好。 Occasionally, the communication to the database is lost, and my log file is drowned in stack traces. 有时,与数据库的通信丢失,并且我的日志文件淹没在堆栈跟踪中。 In this case, I simply want to log that communication was lost, and whenever communication is restored, I want to log that as well. 在这种情况下,我只想记录该通信丢失的情况,每当恢复通信时,我也要记录该消息。

This is an issue, because I can't seem to understand the SQL Exception codes. 这是一个问题,因为我似乎无法理解SQL异常代码。 What I would like to do is log connection loss whenever a connection based exception is caught, and log connection restore whenever I can successfully create a new connection. 我想做的是,每当捕获到基于连接的异常时,日志连接就会丢失,而每当我成功创建新连接时,日志连接就会恢复。 Looking at the log right now, I see that whenever the server is restarted, I catch an exception with number 6005. According to the reference this number indicates: 现在查看日志,我发现无论何时重新启动服务器,我都会捕获到一个编号为6005的异常。 根据引用,该编号表示:

SHUTDOWN is in progress 关机中

This is the exception's message as well, so the exception matches the error code. 这也是该异常的消息,因此该异常与错误代码匹配。

Scrolling further down, I see an exception with 10054 with the following message: 向下滚动,我看到带有以下消息的10054异常:

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. 已成功与服务器建立连接,但是在登录前握手期间发生错误。

but the reference says that error 10054 indicates 但参考资料指出错误10054表示

The data value for one or more columns overflowed the type used by the provider. 一列或多列的数据值溢出了提供程序使用的类型。

This gives me an impression that if I assume that error 10054 is a login issue, I might have problems in case the overflow exception is ever thrown. 这给我的印象是,如果我假设错误10054是登录问题,则在抛出溢出异常的情况下,我可能会遇到问题。 I'd like to note that in my application, the overflow exception should never occur, but i don't want to take that assumption to exception handling, because it is in fact an exception to the regular execution. 我想指出的是,在我的应用程序中,永远不会发生溢出异常,但是我不想将这种假设用于异常处理,因为它实际上是常规执行的异常。

You could simplify this and just create a method to open the database connection for you and catch/handle errors if it fails. 您可以简化此过程,只需创建一种方法即可为您打开数据库连接,并在失败时捕获/处理错误。 Something like this (untested) : 像这样(未经测试)

public bool OpenConnection ()
{
    try 
    {
        using(var connection = new ADODB.Connection(...)) 
        {
            connection.Open();
            return true;
        }
    } 
    catch (Exception ex)
    {
        // log the exception
        return false;
    }
}

Then in your code call that method and handle cases when the connection fails to open: 然后在您的代码中调用该方法并处理连接打开失败的情况:

if (OpenConnection() == false)
{
    // Log / handle / throw error to state the connection failed to open
}
else
{
    // Run the code
}

This approach would still log SQL Exceptions but it will avoid the need to interpret individual exceptions. 这种方法仍然会记录SQL异常,但是它将避免解释各个异常的需要。 Your concern is with connecting to the database, which will either work or not, so this will give you that information and log the exact error. 您所关心的是连接到数据库,该数据库将正常工作或不工作,因此这将为您提供该信息并记录确切的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM