Skip to content

Instantly share code, notes, and snippets.

@tschellenbach
Created September 12, 2012 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tschellenbach/3706224 to your computer and use it in GitHub Desktop.
Save tschellenbach/3706224 to your computer and use it in GitHub Desktop.
Testing transaction managemenet
@fashiolista_env
def health(request):
import random
from django.db import transaction, connections
from user.models import Profile
using = 'default'
def print_isolation_levels(using, msg):
autocommit_sql = "SHOW AUTOCOMMIT;"
isolation_level_sql = "SHOW TRANSACTION ISOLATION LEVEL;"
curs = connections[using].cursor()
curs.execute(autocommit_sql)
autocommit = curs.fetchall()
curs.execute(isolation_level_sql)
isolation_level = curs.fetchall()
psycopg_isolation = connections[using].isolation_level
dirty = transaction.is_dirty(using)
print '%s DB Autocommit: %s, DB isolation %s, Psycopg isolation %s, Dirty %s' % (msg, autocommit, isolation_level, psycopg_isolation, dirty)
#commit manually version
if False:
print_isolation_levels(using, 'pre context manager')
with transaction.commit_manually(using=using):
print_isolation_levels(using, 'in context manager')
profile = request.user.get_profile()
profile.about_me = '%s' % random.choice(range(1000000))
profile.save()
transaction.rollback(using)
print_isolation_levels(using, 'post context manager')
print_isolation_levels(using, 'pre context manager')
try:
with transaction.commit_on_success(using=using):
print_isolation_levels(using, 'in context manager')
profile = request.user.get_profile()
profile.about_me = '%s' % random.choice(range(1000000))
profile.save()
1/0
except ZeroDivisionError, e:
pass
print_isolation_levels(using, 'post context manager')
return HttpResponse(Profile.objects.get(pk=profile.pk).about_me)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment