Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ answer newbie questions, and generally made Django that much better:
Ramon Saraiva <ramonsaraiva@gmail.com>
Ram Rachum <ram@rachum.com>
Randy Barlow <randy@electronsweatshop.com>
Raoni Timo <raoni@relume.io>
Raphaël Barrois <raphael.barrois@m4x.org>
Raphael Michel <mail@raphaelmichel.de>
Raúl Cumplido <raulcumplido@gmail.com>
Expand Down
6 changes: 5 additions & 1 deletion django/db/backends/postgresql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,15 @@ def pool(self):
# Ensure we run in autocommit, Django properly sets it later on.
connect_kwargs["autocommit"] = True
enable_checks = self.settings_dict["CONN_HEALTH_CHECKS"]
# Copy to avoid mutating the user's settings dict.
pool_options = {**pool_options}
pool_options.setdefault(
"check", ConnectionPool.check_connection if enable_checks else None
)
pool = ConnectionPool(
kwargs=connect_kwargs,
open=False, # Do not open the pool during startup.
configure=self._configure_connection,
check=ConnectionPool.check_connection if enable_checks else None,
**pool_options,
)
# setdefault() ensures that multiple threads don't set this in
Expand Down
27 changes: 27 additions & 0 deletions tests/backends/postgresql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,33 @@ def test_pooling_health_checks(self):
finally:
new_connection.close_pool()

@unittest.skipUnless(is_psycopg3, "psycopg3 specific test")
def test_pool_check_can_be_overridden(self):
def custom_check(conn):
pass

new_connection = no_pool_connection(alias="default_pool")
new_connection.settings_dict["OPTIONS"]["pool"] = {"check": custom_check}

for enable_checks in (True, False):
with self.subTest(CONN_HEALTH_CHECKS=enable_checks):
new_connection.settings_dict["CONN_HEALTH_CHECKS"] = enable_checks
try:
self.assertIs(new_connection.pool._check, custom_check)
finally:
new_connection.close_pool()

@unittest.skipUnless(is_psycopg3, "psycopg3 specific test")
def test_pool_options_not_mutated(self):
pool_options = {"min_size": 0, "max_size": 2}
new_connection = no_pool_connection(alias="default_pool")
new_connection.settings_dict["OPTIONS"]["pool"] = pool_options
try:
self.assertIsNotNone(new_connection.pool)
finally:
new_connection.close_pool()
self.assertEqual(pool_options, {"min_size": 0, "max_size": 2})

@unittest.skipUnless(is_psycopg3, "psycopg3 specific test")
def test_cannot_open_new_connection_in_atomic_block(self):
new_connection = no_pool_connection(alias="default_pool")
Expand Down
Loading