Skip to content

Commit

Permalink
fix persistent connections
Browse files Browse the repository at this point in the history
Without autocommit set, BaseDatabaseWrapper.close_if_unusable_or_obsolete()
short-circuits early and always closes the connection after each request.
  • Loading branch information
timgraham committed Feb 12, 2025
1 parent eb26848 commit f5dbacb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django_mongodb_backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _rollback(self):
pass

def set_autocommit(self, autocommit, force_begin_transaction_with_broken_autocommit=False):
pass
self.autocommit = autocommit

@async_unsafe
def close(self):
Expand Down
11 changes: 10 additions & 1 deletion tests/backend_/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.core.exceptions import ImproperlyConfigured
from django.db import connection
from django.test import SimpleTestCase
from django.test import SimpleTestCase, TestCase

from django_mongodb_backend.base import DatabaseWrapper

Expand All @@ -12,3 +12,12 @@ def test_database_name_empty(self):
msg = 'settings.DATABASES is missing the "NAME" value.'
with self.assertRaisesMessage(ImproperlyConfigured, msg):
DatabaseWrapper(settings).get_connection_params()


class DatabaseWrapperConnectionTests(TestCase):
def test_set_autocommit(self):
self.assertIs(connection.get_autocommit(), True)
connection.set_autocommit(False)
self.assertIs(connection.get_autocommit(), False)
connection.set_autocommit(True)
self.assertIs(connection.get_autocommit(), True)

0 comments on commit f5dbacb

Please sign in to comment.