Re: Another question regarding exceptions and loops

From: Julia (codewizard_at_012.net.il)
Date: 10/26/04


Date: Tue, 26 Oct 2004 18:57:27 +0200

Thanks

"Also, what if the operation was an all-or-nothing affair? Where is the
rollback for this kind of thing? Is it in a transaction?"

In this case I define a transaction as saving a single Contact not the List
of all contacts
in such a way if some contact failed to be saved(due to validation rules)
I can still try to save other contacts

 "I think that having separate exceptions for when the list is partially
saved and when it is completely saved is a bad idea, as it will fragment
your code....
I would store it in a Hashtable,
 keyed on the unique identifier for each object (only you know what it is),
and then throw one exception with the hashtable attached to it."

ok,i really dont need separate exceptions

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:u8j04q3uEHA.2136@TK2MSFTNGP15.phx.gbl...
> Julia,
>
> I think that having separate exceptions for when the list is partially
> saved and when it is completely saved is a bad idea, as it will fragment
> your code.
>
> Also, what if the operation was an all-or-nothing affair? Where is
the
> rollback for this kind of thing? Is it in a transaction?
>
> Basically, if there is an exception that is thrown (and I hope you are
> not using them for business logic errors), I would store it in a
Hashtable,
> keyed on the unique identifier for each object (only you know what it is),
> and then throw one exception with the hashtable attached to it.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
> "Julia" <codewizard@012.net.il> wrote in message
> news:%23Qr%23Qj3uEHA.1288@TK2MSFTNGP11.phx.gbl...
> > Thanks for all you responses
> >
> > assuming I have a collection of objects which I want to save in a
> > database
> > I wonder if the following is the way to deal with a situation that only
> > some
> > of the objects were saved
> >
> > function Save()
> > {
> > Exception expList=new Exception() //A list to collect exceptions
> >
> > foreach(Contact contact in Contacts)
> > {
> > try
> > {
> > contact.Persist //Save the current contact
> > }
> > catch(Exception e)
> > {
> > expList.Add(contact,e) //catch the exception for the current
> > object and add it to the list
> > }
> > }
> >
> > if(expList.Count>0)
> > {
> > //Throw exception and attach the list of exceptions
> > throw new PartlyPersistentException("Some objects failed to be
> > saved",expList)
> > }
> > if(expList.Count==Contacts.Count){
> > //Throw exception and attach the list of exceptions
> > throw new PersistentFailedException("All objects failed to be
> > saved",expList)
> > }
> > }
> >
> >
>
>



Relevant Pages

  • BEGIN TRANSACTION problem
    ... there was no BEGIN TRANSACTION. ... The test prior to the exception is the first to execute the parent ... The is the exception the unit test is expecting. ... Rollback statement faile with "The ROLLBACK TRANSACTION request has no ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: How To Tell Youre Within An Exception?
    ... using (Transaction transaction = new Transaction()) ... Rollbackif the current process is currently unwinding an exception ... I want to call Commitwithin the Dispose() ... Allthough in most cases you may rollback on error and commit on success, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Error when calling SqlTransaction.Rollback method
    ... I've seen this when there was an open data reader on the same connection as ... before rolling back the transaction. ... To get the exception, just debug into your code, or alternatively, ... temporarily remove the rollback code or place it in its own try catch. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: JSTL - <sql:transaction>, rollback?
    ... > reason for JSTL to decide to rollback a transaction. ... the exception will be committed if the sql:transaction tag ...
    (comp.lang.java.programmer)
  • Re: Classic Nest SP with Transaction Question
    ... enclosing transaction or should begin and commit/rollback its own ... > I echo Ron's points and also have been using an approach very similar to> Tom's method of using a SAVE PT instead of starting a new TRANSACTION if the> ChildSP is called from the ParentSP. ... > The ParentSp now has to ROLLBACK everything up to that point. ... As Ron> mentioned, the ParentSP may have called many child SPs, and performed many> updates up to this point, and all of these need to be rolled back. ...
    (microsoft.public.sqlserver.programming)