views:

28

answers:

1

Hi, how can I handle exception that is thrown in NHibernate method Flush? I have a action for deleting objects. It loads the objects from repository using posted ids and calls repository.Delete(obj).

Leaving aside that my mapping in NHibernate is not complete and the delete results in "The DELETE statement conflicted with REFERENCE constraint" exception, this is good case to implement the exception hadling for this case.

So the exception is thrown in Flush and the Flush is called in UnitOfWorkPerRequestTask, which is task created and destroyed on every request and it starts and flushes the UnitOfWork (Rhino.Commons.UnitOfWork). This task is registered using Windsor container and LifestyleType.Transient and it is called by HttpApplication inside Begin and EndRequest methods.

Is there any way how to handle this exception in my delete action, so I can notice user that this object cannot be deleted because of some relationships?

The exception stack trace is here (not full):

NHibernate.Transaction.AdoTransaction.Commit() +212
Rhino.Commons.NHibernateTransactionAdapter.Commit() +33
Rhino.Commons.BaseUnitOfWorkFactory.TransactionalFlush(IsolationLevel isolationLevel) +116
Rhino.Commons.BaseUnitOfWorkFactory.TransactionalFlush() +35
Cereal.Mvc.DataModule.Tasks.UnitOfWorkPerRequestTask.DisposeCore() in C:\projects\Sample\Cereal.Mvc.DataModule\Tasks\UnitOfWorkPerRequestTask.cs:33
System.Web.Mvc.Extensibility.Disposable.Dispose(Boolean disposing) in C:\projects\System.Web.Mvc.Extensibility\Abstraction\Disposable.cs:52
System.Web.Mvc.Extensibility.Disposable.Dispose() in C:\projects\System.Web.Mvc.Extensibility\Abstraction\Disposable.cs:35
Castle.MicroKernel.LifecycleConcerns.DisposalConcern.Apply(ComponentModel model, Object component) +47
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.ApplyConcerns(Object[] steps, Object instance) +129
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.ApplyDecommissionConcerns(Object instance) +106
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalDestroy(Object instance) +37
Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Destroy(Object instance) +37
Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Release(Object instance) +48
Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleManager.Evict(Object instance) +38
Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule.Application_EndRequest(Object sender, EventArgs e) +305
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Thanks for help.

+1  A: 

If you call Flush() in a UnitOfWork, you can only catch the error and log it.

If you want your users inform about the error, then close the transaction or do the Flush earlierer (as stated in the comments of Paco and cbp). One place is to handle it in your action method. Check the outcome and report it to the user with an error page.

Christian13467