Skip to content

Conversation

@jdetaeye
Copy link

@jdetaeye jdetaeye commented Dec 9, 2024

The exception handler call "connections.all()". This creates a database connection to all defined databases. As a small optimization you can use "connections.all(initialized_only=True)" to rollback only the database to which the current thread has open connections.

(My application can have many databases defined, and this loop is identified as a source of many idle database connections)

Note: Before submitting a code change, please review our contributing guidelines.

Description

Please describe your pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue. When linking to an issue, please use refs #... in the description of the pull request.

The exception handler call "connections.all()". This creates a database connection to all defined databases. 
As a small optimization you can use "connections.all(initialized_only=True)" to rollback only the database to which the current thread has open connections.

(My application can have many databases defined, and this loop is identified as a source of many idle database connections)
Copy link
Collaborator

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to add tests to avoid regression?

@jdetaeye
Copy link
Author

@auvipy I just looked into updating

def test_class_based_view_exception_handler(self):
with such a test. I failed creating a test case demonstrating the fix (maybe related to the in memory sqlite database used by the test?). Sorry, I won't be able to provide a test.

@stale
Copy link

stale bot commented Apr 26, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 26, 2025
@auvipy auvipy requested a review from Copilot April 27, 2025 08:43
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request optimizes the rollback process by limiting the rollback to initialized database connections only.

  • Updated the set_rollback function to use the "initialized_only" flag
  • Reduces unnecessary database connection instantiation during exception handling

@auvipy auvipy requested a review from a team April 27, 2025 08:44
@stale stale bot removed the stale label Apr 27, 2025
Copy link
Collaborator

@peterthomassen peterthomassen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very reasonable, and I've followed the flow into what Django does when this is called, and it lgtm.

I've also verified that this is supported by all Django releases we rely on. I guess we didn't have this initially because it was only introduced in Django 4.1.

@stale
Copy link

stale bot commented Jul 19, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 19, 2025
@stale stale bot closed this Oct 18, 2025
@auvipy auvipy reopened this Oct 18, 2025
@stale stale bot removed the stale label Oct 18, 2025
@browniebroke browniebroke added this to the 3.17 milestone Oct 20, 2025
@browniebroke browniebroke changed the title set_roll_back should only rollback initialized connections set_rollback should only rollback initialized connections Oct 20, 2025
@browniebroke
Copy link
Collaborator

How does it work with that other PR?

Are they related, complementary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants