Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#10450 closed (fixed)

Savepoint should not assume an opened connection

Reported by: Jeremy Dunck Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Using django transaction.savepoint() without having previously used a cursor fails if the backend actually supports savepoints.

e.g. Using postgres:

Python 2.5.2 (r252:60911, Mar 18 2008, 15:01:36) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.db import transaction
>>> transaction.savepoint()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jeremydunck/work/django/g-django/django/db/", line 188, in savepoint
  File "/Users/jeremydunck/work/django/g-django/django/db/backends/", line 43, in _savepoint
AttributeError: 'NoneType' object has no attribute 'cursor'

It seems this problem has existed since savepoints were introduced, but I found it by trying to use the cached_db sessions backend.

In any case, the problem is that the savepoint family of methods on BaseDatabaseWrapper make the assumption that self.connection has been initiailized-- this is not a safe assumption. self.connection is initialized the first time BaseDatabaseWrapper.cursor is called, and that may not have happened before BaseDatabaseWrapper._savepoint*.

In all cases, ._savepoint* goes on to use connection.cursor, so I think it's safe to just use self.cursor() directly rather than self.connection.cursor().

Patch attached.

Unfortunately, I'm not sure how to make a test for this, because django's test suite itself uses django.db.connection.

At least it is easy to reproduce. :-)

Attachments (1)

savepoint_first.diff (1.0 KB) - added by Jeremy Dunck 10 years ago.
Patch, no test.

Download all attachments as: .zip

Change History (5)

Changed 10 years ago by Jeremy Dunck

Attachment: savepoint_first.diff added

Patch, no test.

comment:1 Changed 10 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:2 Changed 10 years ago by Glenn Maynard

I was just bit by this, too, and I came up with an exactly identical patch.

It also fixes another bug: savepoint commands are hidden from the debug cursor, so they never show up in SQL logs.

comment:3 Changed 10 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [10512]) [1.0.X] Fixed #10450 -- Fixed an initialisation problem in the savepoint code.

Patch from Jeremy Dunck.

Backport of r10511 from trunk.

comment:4 Changed 7 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top