NHibernate / Castle activerecord:如何获取导致数据库异常的对象?

发布于 2024-07-26 23:16:22 字数 186 浏览 1 评论 0原文

我能否以某种方式获取导致 GenericADOException(约束异常)的对象?

或者我怎样才能只刷新一个对象,这样我就可以知道哪个对象出了问题。

我有一个以表单显示的对象列表,可以编辑和添加。 在刷新时,它给了我一个数据库异常,但我无法分辨哪个对象给出了异常。

我无法将约束移至休眠状态。

Can I somehow get the object that caused a GenericADOException (constraint exception)?

Or How can I only flush one object so I can tell which one is the problem.

I have a list of object that are displayed in a form, and can be edited and added to. On flush it gives me a database exception but I can't tell which object gave the exception.

I can't move the constraint to nhibernate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-08-02 23:16:22

您尝试过使用 NHibernate Profiler 吗? 它应该为您提供有关问题所在的更多详细信息。

Have you tried using NHibernate Profiler? It should give you additional details as to which one is the problem.

黎歌 2024-08-02 23:16:22

通过一些谷歌搜索,我找到了一篇有用的帖子,然后通过阅读 nhibernate 源代码,我得到了以下内容:

http://fabiomaulo.blogspot.com/2009/06/improving-ado-exception-management-in.html


'http://fabiomaulo.blogspot.com/2009/06/improving-ado-exception-management-in.html
'properties.Add("sql_exception_converter", "SmartCore.SmartDatabaseExceptionConverter, SmartCore")
Class SmartDatabaseExceptionConverter
    Implements ISQLExceptionConverter


    Public Function Convert(ByVal sqlException As System.Exception, ByVal message As String, ByVal sql As NHibernate.SqlCommand.SqlString) As NHibernate.ADOException Implements NHibernate.Exceptions.ISQLExceptionConverter.Convert

        Dim sqle As DbException
        sqle = ADOExceptionHelper.ExtractDbException(sqlException)
        '"could not update: [SmartCore.GL.Glaccount#1225]"
        Dim obname As String
        Dim key As String
        obname = ExtractUsingTemplate("could not update: [", "#", message)
        key = ExtractUsingTemplate("#", "]", message)

        Dim prikey As Integer
        prikey = Integer.Parse(key)

        sqle.Data.Add("obname", obname)
        sqle.Data.Add("prikey", key)

        If sqle.ErrorCode = 335544558 Then
            '"Operation violates CHECK constraint C_GLACCOUNT on view or table GLACCOUNT At trigger 'CHECK_56'"
            Dim checkname As String
            checkname = ExtractUsingTemplate("Operation violates CHECK constraint ", "on view or table ", sqle.Message)
            Return New SmartDatabaseConstraintException(message, sqle, obname, prikey, checkname)
            'Return New ConstraintViolationException(message, sqle, sql, checkname)
        End If

        Return SQLStateConverter.HandledNonSpecificException(sqlException, message, sql)
    End Function



    Protected Function ExtractUsingTemplate(ByVal templateStart As String, ByVal templateEnd As String, ByVal message As String) As String
        Dim templateStartPosition As Integer = message.IndexOf(templateStart)
        If templateStartPosition < 0 Then
            Return Nothing
        End If

        Dim start As Integer = templateStartPosition + templateStart.Length
        Dim [end] As Integer = message.IndexOf(templateEnd, start)
        If [end] < 0 Then
            [end] = message.Length
        End If

        Return message.Substring(start, [end] - start).Trim()

    End Function
End Class

'USAGE
        Try
            _scope.Flush()
        Catch ex As SmartDatabaseConstraintException
            If ex.ConstraintName = "C_GLACCOUNT" Then
                Dim gla As GL.Glaccount
                gla = GL.Glaccount.Find(ex.EntityId) 'should be fast from entity cache
                msgboxError(gla.description & " is duplicate please rename")
            Else
                msgboxError(ex.Message)
            End If
        End Try

我必须调整它以适应我的工作稍微过时的 nhibernate 版本(3 月份的 2.1)我将不得不稍微调整上面的内容以适应较新的版本,其中有一个 AdoExceptionContextInfo 可以从中获取对象名称和 ID。 冬眠不错啊!

By some googling I got to a post that was helpful, then by reading the nhibernate source I got to the following:

http://fabiomaulo.blogspot.com/2009/06/improving-ado-exception-management-in.html


'http://fabiomaulo.blogspot.com/2009/06/improving-ado-exception-management-in.html
'properties.Add("sql_exception_converter", "SmartCore.SmartDatabaseExceptionConverter, SmartCore")
Class SmartDatabaseExceptionConverter
    Implements ISQLExceptionConverter


    Public Function Convert(ByVal sqlException As System.Exception, ByVal message As String, ByVal sql As NHibernate.SqlCommand.SqlString) As NHibernate.ADOException Implements NHibernate.Exceptions.ISQLExceptionConverter.Convert

        Dim sqle As DbException
        sqle = ADOExceptionHelper.ExtractDbException(sqlException)
        '"could not update: [SmartCore.GL.Glaccount#1225]"
        Dim obname As String
        Dim key As String
        obname = ExtractUsingTemplate("could not update: [", "#", message)
        key = ExtractUsingTemplate("#", "]", message)

        Dim prikey As Integer
        prikey = Integer.Parse(key)

        sqle.Data.Add("obname", obname)
        sqle.Data.Add("prikey", key)

        If sqle.ErrorCode = 335544558 Then
            '"Operation violates CHECK constraint C_GLACCOUNT on view or table GLACCOUNT At trigger 'CHECK_56'"
            Dim checkname As String
            checkname = ExtractUsingTemplate("Operation violates CHECK constraint ", "on view or table ", sqle.Message)
            Return New SmartDatabaseConstraintException(message, sqle, obname, prikey, checkname)
            'Return New ConstraintViolationException(message, sqle, sql, checkname)
        End If

        Return SQLStateConverter.HandledNonSpecificException(sqlException, message, sql)
    End Function



    Protected Function ExtractUsingTemplate(ByVal templateStart As String, ByVal templateEnd As String, ByVal message As String) As String
        Dim templateStartPosition As Integer = message.IndexOf(templateStart)
        If templateStartPosition < 0 Then
            Return Nothing
        End If

        Dim start As Integer = templateStartPosition + templateStart.Length
        Dim [end] As Integer = message.IndexOf(templateEnd, start)
        If [end] < 0 Then
            [end] = message.Length
        End If

        Return message.Substring(start, [end] - start).Trim()

    End Function
End Class

'USAGE
        Try
            _scope.Flush()
        Catch ex As SmartDatabaseConstraintException
            If ex.ConstraintName = "C_GLACCOUNT" Then
                Dim gla As GL.Glaccount
                gla = GL.Glaccount.Find(ex.EntityId) 'should be fast from entity cache
                msgboxError(gla.description & " is duplicate please rename")
            Else
                msgboxError(ex.Message)
            End If
        End Try

I had to adapt it to work with my slightly outdated nhibernate version (2.1 from march) I will have to adapt the above slightly to work with newer versions where there is a AdoExceptionContextInfo from which one can get the object name and ID. Nhibernate is good!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文