Interface Cursor Already Closed on Nginx

Python Django

Saat kamu menggunakan raw query pada django dengan database postgres dan kamu menemukan pesan error “Interface Cursor Already Closed”. kamu perlu membuat Try … Exception, pesan error tersebut di karenakan akses koneksi ke database terputus dan pid uwsgi tidak mampu mereload koneksi ke database postgres. solusi simplenya lihat contoh di bawah ini

from django.db import connections
from django.db.utils import InterfaceError
cursor = connections['default'].cursor()
 
def dictfetchall(cursor):
    "Return all rows from a cursor as a dict"
    columns = [col[0] for col in cursor.description]
    return [
        dict(zip(columns, row))
        for row in cursor.fetchall()
        ]
try:
    cursor.execute("""select * from ean_expedia_properties""")
    all_list = dictfetchall(cursor)    
except InterfaceError:
    cr = connections['default'].cursor()
    cr.execute("""select * from ean_expedia_properties""")
    all_list = dictfetchall(cr)
 
print(all_list)

 

good luck !

mm

Alfin F

Technopreneur , Developer, Advisor

More Posts

Follow Me:
TwitterLinkedIn