#22420: Postgresql connections not being dropped between tests?
-----------------------------------+--------------------------------------
     Reporter:  Matthew Fisher     |                    Owner:  nobody
         Type:  Bug                |                   Status:  closed
    Component:  Testing framework  |                  Version:  3.0
     Severity:  Normal             |               Resolution:  needsinfo
     Keywords:                     |             Triage Stage:  Unreviewed
    Has patch:  0                  |      Needs documentation:  0
  Needs tests:  0                  |  Patch needs improvement:  0
Easy pickings:  0                  |                    UI/UX:  0
-----------------------------------+--------------------------------------

Comment (by Ryan Causey):

 I found this code snippet in some old tests I was perusing recently. I
 hope it helps someone.

 {{{#!python
 def close_db_connections(func, *args, **kwargs):
     """
     Decorator to explicitly close db connections during threaded execution

     Note this is necessary to work around:
     https://code.djangoproject.com/ticket/22420
     """

     def _close_db_connections(*args, **kwargs):
         ret = None
         try:
             ret = func(*args, **kwargs)
         finally:
             for conn in connections.all():
                 conn.close()
         return ret

     return _close_db_connections

 # This decorator is necessary to work around:
 # https://code.djangoproject.com/ticket/22420
 @close_db_connections
 def create_account():
     """Create an account for the test_user."""
     account = Account(
         name="foo",
         jurisdiction=test_jurisdiction,
         alarm_system_address=test_address,
         alarm_system_user=test_user,
         effective_creation_date=timezone.now().date(),
     )
     account.full_clean()
     account.save()
     return account

 futures = []
 with ThreadPoolExecutor() as executor:
     # Try to run one thousand account creations concurrently.
     for _ in range(1000):
         futures.append(executor.submit(create_account))
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22420#comment:28>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.9d43d73a3cb13ca032c2ceb61ef0037a%40djangoproject.com.

Reply via email to