代码之家  ›  专栏  ›  技术社区  ›  AngelBlaZe

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

  •  1
  • AngelBlaZe  · 技术社区  · 15 年前

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

    或者我怎样才能只冲洗一个物体,这样我就能分辨出哪个是问题所在。

    我有一个以窗体显示的对象列表,可以对其进行编辑和添加。在flush时,它会给我一个数据库异常,但我无法分辨哪个对象给出了异常。

    我无法将约束移动到nhibernate。

    2 回复  |  直到 15 年前
        1
  •  0
  •   Shane Courtrille    15 年前

    你试过使用NHibernate分析仪吗?它应该为您提供问题所在的其他详细信息。

        2
  •  0
  •   AngelBlaZe    15 年前

    通过一些谷歌搜索,我找到了一个有用的帖子,然后通过阅读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版本(2.1从3月起)一起工作,我将不得不稍微调整上面的版本,以便与更新的版本一起工作,其中有一个adoExceptionContextInfo,从中可以获得对象名称和ID。nhibernate是好的!

    推荐文章