@transaction.commit_on_success, not getting expected results

2011-10-05 Thread robinne
Based on Django 1.3 documentation for transactions, it seems you can
have all or nothing updates of database calls within a view. I am not
getting the expected behavior and it seems there must be an
"autocommit" global setting somewhere that I need to set to false.
Does anyone know of such a setting? Here is sample code, I would
expect no updates to my "Credit" table, yet I get one new row.

@transaction.commit_on_success
def Test(request):
try:
member = request.user.get_profile()
Credit.objects.create(CreditDate=datetime.datetime.now(),
Member=member, CreditAmount=25)
Credit.objects.create(CreditDate=datetime.datetime.now(),
Member=member, CreditAmount=25.0) #FAIL

return render_to_response("Test.html")
except(Exception),e:
WriteToLog('error in test: ' + e.__str__())
return HttpResponseServerError('error')

Do I need to use @transaction.commit_manually instead? I know that
gives me the expected results but is a lot more work to update all my
code with manual commits and rollbacks. thanks.

-- 
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.



Re: @transaction.commit_on_success, not getting expected results

2011-10-05 Thread Javier Guerra Giraldez
On Wed, Oct 5, 2011 at 8:15 PM, robinne  wrote:
> @transaction.commit_on_success
> def Test(request):
>    try:
>        member = request.user.get_profile()
>        Credit.objects.create(CreditDate=datetime.datetime.now(),
> Member=member, CreditAmount=25)
>        Credit.objects.create(CreditDate=datetime.datetime.now(),
> Member=member, CreditAmount=25.0) #FAIL
>
>        return render_to_response("Test.html")
>    except(Exception),e:
>        WriteToLog('error in test: ' + e.__str__())
>        return HttpResponseServerError('error')

from the docs [1]:

"If the function returns successfully, then Django will commit all
work done within the function at that point. If the function raises an
exception, though, Django will roll back the transaction."

but you're catching any exception and returning an HttpResponse.  from
the point of view of Python, that's a successful function return, so
the transaction is committed.

if you want both: transaction rollback, and exception catching (to
turn them into log+HttpError), i think you should write your own
decorator, maybe something like (untested!):

def rollback_and_error(f, *args, **kwargs):
try:
with transaction.commit_on_success():
return f(*args, **kwargs)
except(Exception),e:
WriteToLog('error in %s: %s' %(f.__name__, e.__str__()))
return HttpResponseServerError('error')


[1]:https://docs.djangoproject.com/en/1.3/topics/db/transactions/#django.db.transaction.commit_on_success

-- 
Javier

-- 
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.