My apologies, poorly written question (was late on Friday and I should
have gone home!)

What I'm doing is writing several records to the database in a single
request.  The errors may occur due to validation problems and I'm
checking for these as I go.  However the problem occurs as follows:

validate record1
success - write record1

validate record2
success - write record2

validate record3
error - return validation error to user

However, the first two records have already been written and because
no exception occured the transaction would be committed.

The solution here of course is to do this:

validate record1
validate record2
validate record3

if all okay then
  write record1
  write record2
  write record3

I was a bit slow last week and this obvious solution didn't present
itself to me.

However, I am still curious if there is a way to tell django to not
commit the transaction without the user seeing an exception????

thanks!


On Jun 26, 11:13 pm, Karen Tracey <kmtra...@gmail.com> wrote:
> On Fri, Jun 26, 2009 at 12:59 AM, sico <allensi...@gmail.com> wrote:
>
> > Hi
>
> > Is it possible to rollback the transaction without raising an
> > exception when the transactions are tied to http requests??
> > I'm writing several records in one post call, if any fail I'd like to
> > rollback the transaction but return a nice error message to the user.
> > Surely I'm missing something as this seems quite a simple
> > requirement...
>
> You haven't given any indication of what you had tried here.  Since you are
> asking the question I gather whatever you have tried hasn't worked, but I'm
> not sure where you are running into trouble since I don't know what you've
> done.
>
> First, if you want to be able to roll back some already-completed operations
> due to a subsequent one failing, you will need to use something other than
> Django's default autocommit behavior, as described here:
>
> http://docs.djangoproject.com/en/dev/topics/db/transactions/#topics-d...
>
> The commit_manually decorator is probably what you want to use.
>
> Second, if you want to recover from attempting something that causes a
> database error, your code needs to catch whatever database exception is
> raised and recover.  Something like:
>
>     try:
>         instance = new_model.save()
>     except IntegrityError:
>         transaction.rollback()
>         ... whatever else you want to do to inform the user ...
>
> Your code must be prepared to handle the possible database errors, there is
> no setting to tell the framework to auomagically eat them and rollback the
> transaction (if it did, how would you know that had happened?).
>
> Karen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to