diff --git a/api/CHANGELOG.md b/api/CHANGELOG.md index 868373edc0..88ab6c9c88 100644 --- a/api/CHANGELOG.md +++ b/api/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to the **Prowler API** are documented in this file. ## [v1.8.3] (Prowler v5.7.3) +### Added +- Database backend to handle already closed connections [(#7935)](https://github.com/prowler-cloud/prowler/pull/7935). + ### Fixed - Fixed transaction persistence with RLS operations [(#7916)](https://github.com/prowler-cloud/prowler/pull/7916). - Reverted the change `get_with_retry` to use the original `get` method for retrieving tasks [(#7932)](https://github.com/prowler-cloud/prowler/pull/7932). diff --git a/api/src/backend/config/django/base.py b/api/src/backend/config/django/base.py index 995e2b946d..92e7da0a99 100644 --- a/api/src/backend/config/django/base.py +++ b/api/src/backend/config/django/base.py @@ -127,6 +127,7 @@ DJANGO_GUID = { } DATABASE_ROUTERS = ["api.db_router.MainRouter"] +POSTGRES_EXTRA_DB_BACKEND_BASE = "database_backend" # Password validation diff --git a/api/src/backend/database_backend/__init__.py b/api/src/backend/database_backend/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/api/src/backend/database_backend/base.py b/api/src/backend/database_backend/base.py new file mode 100644 index 0000000000..69fee7df4b --- /dev/null +++ b/api/src/backend/database_backend/base.py @@ -0,0 +1,15 @@ +import django.db +from django.db.backends.postgresql.base import ( + DatabaseWrapper as BuiltinPostgresDatabaseWrapper, +) +from psycopg2 import InterfaceError + + +class DatabaseWrapper(BuiltinPostgresDatabaseWrapper): + def create_cursor(self, name=None): + try: + return super().create_cursor(name=name) + except InterfaceError: + django.db.close_old_connections() + django.db.connection.connect() + return super().create_cursor(name=name)