Transaction commit with cursor.execute

1,368 views
Skip to first unread message

idle sign

unread,
Apr 28, 2010, 12:59:50 PM4/28/10
to Django users
I think I spotted something weird. May be someone could explain that?

1. In Django 1.2 define two DBs (let it be sqlite), one of which name
'test'.
2. Define DB router for 'testapp' so that it always uses 'test' DB.
3. Use 'commit_manually' decorator for 'test' view.
4. In 'test' view define 'cursor' pointing to 'test' DB.
5. Execute some INSERTs and commit
*. 'Samples' object (exported from model) would show all inserted
object, but nothing would be written into DB.

@transaction.commit_manually(using='test')

def test(request):



print Samples.objects.all()



cursor = connections['test'].cursor()



cursor.execute("PRAGMA temp_store=MEMORY")

cursor.execute("PRAGMA synchronous=OFF")



for i in range(1, 25):

cursor.execute("INSERT INTO testapp_samples (simplefield)
VALUES (%s);", [i])



transaction.commit()



print Samples.objects.all()



raise Http404

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Russell Keith-Magee

unread,
Apr 28, 2010, 10:07:38 PM4/28/10
to django...@googlegroups.com
On Thu, Apr 29, 2010 at 12:59 AM, idle sign <idle...@gmail.com> wrote:
> I think I spotted something weird. May be someone could explain that?
>
> 1. In Django 1.2 define two DBs (let it be sqlite), one of which name
> 'test'.
> 2. Define DB router for 'testapp' so that it always uses 'test' DB.
> 3. Use 'commit_manually' decorator for 'test' view.
> 4. In 'test' view define 'cursor' pointing to 'test' DB.
> 5. Execute some INSERTs and commit
> *. 'Samples' object (exported from model) would show all inserted
> object, but nothing would be written into DB.

The problem is on this line:

>    transaction.commit()

By default (for backwards compatibility), commit() operates on the
default database. If you want to commit results on the 'test'
connection, you need to provide a 'using' argument:

transaction.commit(using='test')

Yours,
Russ Magee %-)

idle sign

unread,
Apr 28, 2010, 11:04:58 PM4/28/10
to Django users
I thought so, have tried so, but got "This code isn't under
transaction management", and opened this thread :)

Environment:

Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.2 beta 1
Python Version: 2.6.4
Installed Applications:
['testapp']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/python2.6/dist-packages/django/core/handlers/base.py"
in get_response
100. response = callback(request,
*callback_args, **callback_kwargs)
File "/home/idle/projects/gitdev/django-tests/testapp/views.py" in
test
22. transaction.commit(using='test')
File "/usr/lib/python2.6/dist-packages/django/db/transaction.py" in
commit
200. set_clean(using=using)
File "/usr/lib/python2.6/dist-packages/django/db/transaction.py" in
set_clean
125. raise TransactionManagementError("This code isn't under
transaction management")

Exception Type: TransactionManagementError at /
Exception Value: This code isn't under transaction management


On 29 апр, 09:07, Russell Keith-Magee <freakboy3...@gmail.com> wrote:

Russell Keith-Magee

unread,
Apr 28, 2010, 11:16:30 PM4/28/10
to django...@googlegroups.com
On Thu, Apr 29, 2010 at 11:04 AM, idle sign <idle...@gmail.com> wrote:
> I thought so, have tried so, but got "This code isn't under
> transaction management", and opened this thread :)

I get that error if I *omit* the using= argument to commit (which is
what I'd expect to see).

What version of trunk are you using? There have been a couple of
bugfixes to the commit decorators since the beta, and it's possibly
you might be tripping over one of them.

Failing that - I can only assume there is something unusual with your
database or router setup that is causing a complication. I'd need to
see specifics (for example, a complete test project) to verify if
there is something else going on.

idle sign

unread,
Apr 29, 2010, 12:20:05 AM4/29/10
to Django users
I'm using the latest trunk from http://github.com/django/django.
A complete test project you can grab at http://idlesign.narod.ru/django/django-tests.tar.gz
There is a little bootstrap.sh to run to create DBs.

Thank you, Russ.


On 29 апр, 10:16, Russell Keith-Magee <freakboy3...@gmail.com> wrote:

Russell Keith-Magee

unread,
Apr 29, 2010, 2:16:12 AM4/29/10
to django...@googlegroups.com
On Thu, Apr 29, 2010 at 12:20 PM, idle sign <idle...@gmail.com> wrote:
> I'm using the latest trunk from http://github.com/django/django.
> A complete test project you can grab at http://idlesign.narod.ru/django/django-tests.tar.gz
> There is a little bootstrap.sh to run to create DBs.
>
> Thank you, Russ.

Ok - using your test application as provided, I see the "This code
isn't under transaction management" error. That's because line 7 of
views.py (the decorator that enables transaction management) is
commented out. When line 7 is uncommented, transaction management is
enabled, and the view works as I would expect.

I take it you seeing something different?
Message has been deleted
Message has been deleted

idle sign

unread,
Apr 29, 2010, 3:04:23 AM4/29/10
to Django users
> When line 7 is uncommented, transaction management is
> enabled, and the view works as I would expect.

So it is, my bad, thank you againg.

You see, it seemed a little weird, that first (with the decorator) we
force transaction to commit manually to use 'test' for all that would
happen in our view (suppose that all commits in view should share that
'using' from the decorator), and then explicitly tell the transaction
to commit again with 'using'. But if we have transactions to different
DBs in one view that seems logical.

On 29 апр, 13:16, Russell Keith-Magee <freakboy3...@gmail.com> wrote:
> On Thu, Apr 29, 2010 at 12:20 PM, idle sign <idles...@gmail.com> wrote:
> > I'm using the latest trunk fromhttp://github.com/django/django.
> > A complete test project you can grab athttp://idlesign.narod.ru/django/django-tests.tar.gz
Reply all
Reply to author
Forward
0 new messages