如何获取NHibernate异常的更新SQL语句?
我正在处理将某个聚合保存到数据库时遗留代码。 来自SQL服务器的错误是着名的'SqlDateTime溢出。 必须在1/1/1753之间...等'
我的问题是:是否有可能问NHibernate框架什么SQL执行冒泡这个异常? 通过这种方式,我将可以轻松找到POCO所需要解决的问题。
是否有可能将一个监听器附加到NHibernate的IDbCommand或其他东西?
以下例外的堆栈跟踪示例:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. StackTrace: at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc, Boolean sync, TaskCompletionSource`1 completion, Int32 startRpc, Int32 startParam)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.SqlClient.SqlCommandSet.ExecuteNonQuery()
at NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand ps)
at NHibernate.AdoNet.AbstractBatcher.ExecuteBatchWithTiming(IDbCommand ps)
at NHibernate.AdoNet.AbstractBatcher.ExecuteBatch()
at NHibernate.AdoNet.AbstractBatcher.PrepareCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
at NHibernate.Action.EntityUpdateAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
at NHibernate.Engine.ActionQueue.ExecuteActions()
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
at Haddock.Business.Services.CommonRepositoryBase`3.SaveOrUpdate(TRootAggregate rootAggregate) in d:Builds4HaddockHaddock.Releases.P44-2014.SLSourcesBusinessHaddock.Business.ServicesCommonRepositoryBase.cs:line 140
...
我正在考虑在CommonRepositoryBase类中添加一些额外的检查代码,该代码将在整个项目中使用。
这里是代码,这实际上是可以理解的:
ISession session = GetSession();
using (ITransaction transaction = session.BeginTransaction())
{
try
{
session.SaveOrUpdate(rootAggregate);
transaction.Commit(); // does a flush internally
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
因此,我的目标是在该Catch Block中添加一些代码,以检查正在使用的会话和事务,并使用NHibernate希望执行的SQL语句扩展该Exception。
您可以在SQL Server Management Studio中使用SQL Server Profiler来跟踪正在数据库上运行的查询。
只需在复制bug之前启动分析器,并且您可以从那里看到执行的查询。
除了查询转储之外,您还可以查看执行查询的更多细节,如执行时间等。
链接地址: http://www.djcxy.com/p/26971.html上一篇: How to get the Update SQL statement out of NHibernate exception?