2009-07-16 54 views
1

我可以以某種方式獲取導致GenericADOException(約束異常)的對象嗎?NHibernate/Castle activerecord:如何獲取導致數據庫異常的對象?

或者我怎樣才能刷新一個對象,以便我可以告訴哪一個是問題。

我有一個對象的列表顯示在窗體中,可以編輯和添加到。在刷新它給我一個數據庫異常,但我不知道哪個對象給出了例外。

我無法將約束移動到nhibernate。

回答

0

通過一些谷歌上搜索我的職位,是有幫助的,然後通過讀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。 Nhibernate很好!

0

您是否嘗試過使用NHibernate Profiler?它應該給你更多的細節,問題是哪一個。

+0

我通過眼球知道哪一個導致了問題,問題是如何通過代碼來確定。我需要在代碼中知道哪些對象正在給出問題,以便我可以修改或刪除它。 考慮數據庫中的唯一約束。 – AngelBlaZe 2009-07-16 14:09:33

相關問題