Skip to content

Commit c7831c6

Browse files
committed
Prohibit deactivating transaction management with AUTOCOMMIT=False in DATABASES
Unlike SQL databases, MongoDB has no notion of "deactivating transaction management". A side effect of this backend's implementation is that this configuration starts a transaction upon connecting to the database.
1 parent 7d25f27 commit c7831c6

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

django_mongodb_backend/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ def __init__(self, settings_dict, alias=DEFAULT_DB_ALIAS):
159159
super().__init__(settings_dict, alias=alias)
160160
self.session = None
161161

162+
def check_settings(self):
163+
super().check_settings()
164+
if not self.settings_dict["AUTOCOMMIT"]:
165+
raise ImproperlyConfigured("MongoDB does not support AUTOCOMMIT=False.")
166+
162167
def get_collection(self, name, **kwargs):
163168
collection = Collection(self.database, name, **kwargs)
164169
if self.queries_logged:

docs/source/ref/database.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ default behavior of autocommit mode. Each query is immediately committed to the
5858
database. Django's transaction management APIs, such as
5959
:func:`~django.db.transaction.atomic`, function as no-ops.
6060

61+
:ref:`Deactivating transaction management <django:deactivate-transaction-management>`
62+
by setting :setting:`AUTOCOMMIT <DATABASE-AUTOCOMMIT>` to ``False`` in the
63+
:setting:`DATABASES` setting isn't supported.
64+
6165
.. _transactions-limitations:
6266

6367
Limitations

tests/backend_/test_base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import copy
2+
13
from django.core.exceptions import ImproperlyConfigured
24
from django.db import connection
35
from django.db.backends.signals import connection_created
@@ -14,6 +16,14 @@ def test_database_name_empty(self):
1416
with self.assertRaisesMessage(ImproperlyConfigured, msg):
1517
DatabaseWrapper(settings).get_connection_params()
1618

19+
def test_autocommit_false(self):
20+
new_connection = connection.copy()
21+
new_connection.settings_dict = copy.deepcopy(connection.settings_dict)
22+
new_connection.settings_dict["AUTOCOMMIT"] = False
23+
msg = "MongoDB does not support AUTOCOMMIT=False."
24+
with self.assertRaisesMessage(ImproperlyConfigured, msg):
25+
new_connection.check_settings()
26+
1727

1828
class DatabaseWrapperConnectionTests(TransactionTestCase):
1929
available_apps = ["backend_"]

0 commit comments

Comments
 (0)