异常处理

Nuodb支持从常见错误状态映射到分类的SQL异常。 这允许JDBC程序员更好地处理特定的SQL错误状态。 There are two types of categorized SQL exceptions thrown by NuoDB: SQLNonTransientException and SQLTransientException. To obtain more information about an error, use SQLException#getErrorCode().

非瞬态SQL例外

A non-transient SQLException is thrown when a retry of the same operation would fail unless the cause of the SQL exception is corrected. 在接收到非瞬态SQL异常后,应用程序可以假设连接仍然有效。

sqlstate. nontransient sqlexception.

08

SQLNonTransientConnectionException on initial attempt (i.e. Driver#connect())

0A

SQLFeatureNotSupportedException

22

SQLDataException

23

SQLIntegrityConstraintViolationException

28

SQLInvalidAuthorizationException

42

SQLSyntaxErrorException

瞬态SQL例外

A transient SQLException is thrown when a previously failed operation might be able to succeed when the operation is retried without any intervention by application-level functionality.

sqlstate. 短暂的Sqlexception.

08

SQLTransientConnectionException 尝试我们曾经有联系但不再这样做

40

SQLTransactionRollbackException

获取错误信息

You can use the SQLException.getErrorCode() function to obtain more information about an error.

  • 例如,您可能需要区分唯一的约束违规和检查约束违规。 处理异常时,请检查错误代码。

    • 对于唯一的约束违规,错误代码为-27。

    • 检查约束违规时错误代码为-45。

  • SQL错误代码.

例如:

try {
    preparedStatement.execute();
} catch (SQLException e) {
    if (e.getErrorCode() == SQLState.UNIQUE_DUPLICATE.getCode()) {
        System.err.println("Unexpected duplicate");
    } else if (e.getErrorCode() == SQLState.CONSTRAINT_ERROR.getCode()) {
        System.err.println("Constraint violation");
    } else {
        System.err.println("Unexpected error: \"" + e.getMessage() + "\"");
    }
}

batchupdateexception.

batchupdateexception. supports exception chaining. Invoking getNextException() repeatedly on a batchupdateexception. object, it will traverse the list of exceptions that were generated.

Here is an example catch clause for a batchupdateexception.:

   ....
    } catch (BatchUpdateException bue) {

        int[] updateCounts = bue.getUpdateCounts();
        SQLException exception = bue;
        for (int i = 0; i < updateCounts.length; i++) {
            System.out.println(
                "** BatchUpdateException updateCounts[" + i + "]=\"" +
                updateCounts[i] + "\"" );
            if (updateCounts[i] == java.sql.Statement.EXECUTE_FAILED) {
                exception = exception.getNextException();
                System.out.println("   SQLException \"" + exception + "\"");
            }
        }
    }

假设来自此示例的输出看起来如下所示:

** BatchUpdateException updateCounts[0]="1"
** BatchUpdateException updateCounts[1]="1"
** BatchUpdateException updateCounts[2]="-3"
   SQLException "java.sql.SQLIntegrityConstraintViolationException: violation of constraint "GENDER""
** BatchUpdateException updateCounts[3]="1"
** BatchUpdateException updateCounts[4]="-3"
   SQLException "java.sql.SQLIntegrityConstraintViolationException: duplicate value in unique index MEMBERS..PRIMARY_KEY, key = '4'"
** BatchUpdateException updateCounts[5]="1"

这会告诉我们

  • 此批处理更新执行了六个语句。

    • NuodB将始终在批量更新中继续执行语句,即使发生错误。

  • 批处理中的第3个语句导致错误
    java.sql.SQLIntegrityConstraintViolationException: violation of constraint "GENDER"

  • 批处理中的第5个语句导致错误
    java.sql.SQLIntegrityConstraintViolationException: duplicate value in unique index MEMBERS..PRIMARY_KEY, key = '4'.