1

I use MongoDB as the Django's database and I use djongo for operation, it seems that it will close the connection automatically and can't restore. For example, I create a simple view to show all the records like before:

book_res = Book.object.all()
...
return Response(res)

It will work in the first time I get to the view's url address, if i refresh the page or redirect into the same url, the system will crash and shows the error message that "cannot use mongoclient after close" that cause an ambigous error "django.db.utils.DatabseError"

Here is the full trace back error, by the way, this url will work when I first get it, this error happens when refresh

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 808, in __iter__
    yield from iter(self._query)
  File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 166, in __iter__
    for doc in cursor:
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\cursor.py", line 1248, in next
    if len(self.__data) or self._refresh():
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\cursor.py", line 1165, in _refresh
    self.__send_message(q)
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\cursor.py", line 1052, in __send_message
    response = client._run_operation(
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\mongo_client.py", line 1267, in _run_operation
    return self._retryable_read(
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\mongo_client.py", line 1364, in _retryable_read
    server = self._select_server(read_pref, session, address=address)
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\mongo_client.py", line 1186, in _select_server
    topology = self._get_topology()
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\mongo_client.py", line 1143, in _get_topology
    self._topology.open()
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\topology.py", line 188, in open
    self._ensure_opened()
  File "C:\Program Files (x86)\Python38\lib\site-packages\pymongo\topology.py", line 573, in _ensure_opened
    raise InvalidOperation("Cannot use MongoClient after close")

The above exception (Cannot use MongoClient after close) was the direct cause of the following exception:
  File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\cursor.py", line 65, in fetchmany
    ret.append(self.result.next())
  File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 797, in __next__
    result = next(self._result_generator)
  File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\sql2mongo\query.py", line 830, in __iter__
    raise exe from e

The above exception (

    Keyword: FAILED SQL: SELECT "ey_io_compname"."ey_inputName", "ey_io_compname"."ey_outputName" FROM "ey_io_compname" WHERE "ey_io_compname"."ey_inputName" = %(0)s LIMIT 21
Params: ('上海伟仕佳杰科技有限公司',)
Version: 1.3.6
    Sub SQL: None
    FAILED SQL: None
    Params: None
    Version: None) was the direct cause of the following exception:
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\utils.py", line 98, in inner
    return func(*args, **kwargs)
  File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\cursor.py", line 70, in fetchmany
    raise db_exe from e

The above exception () was the direct cause of the following exception:
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\views\generic\base.py", line 84, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Program Files (x86)\Python38\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Program Files (x86)\Python38\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Program Files (x86)\Python38\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Program Files (x86)\Python38\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Trigger\project\EBISre\apis\views.py", line 62, in get
    print(data_ormio)
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\query.py", line 296, in __repr__
    data = list(self[: REPR_OUTPUT_SIZE + 1])
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\query.py", line 302, in __len__
    self._fetch_all()
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\query.py", line 1507, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\query.py", line 130, in __iter__
    for row in compiler.results_iter(
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1312, in results_iter
    results = self.execute_sql(
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1394, in execute_sql
    return list(result)
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1873, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1873, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\utils.py", line 98, in inner
    return func(*args, **kwargs)
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Program Files (x86)\Python38\lib\site-packages\django\db\utils.py", line 98, in inner
    return func(*args, **kwargs)
  File "C:\Program Files (x86)\Python38\lib\site-packages\djongo\cursor.py", line 70, in fetchmany
    raise db_exe from e

Exception Type: DatabaseError at /api/verifyCompanyRes/
Exception Value: 
Selvaria
  • 11
  • 2
  • You will need to show us the traceback for the error message. It's impossible to pinpoint the problem otherwise. – AKX Apr 25 '22 at 07:24
  • Thanks for the reminder, I've already add the traceback for the error message. – Selvaria Apr 27 '22 at 14:08

1 Answers1

0

Just to make sure, did you ran:

python manage.py makemigrations

And

python manage.py migrate
B. Okba
  • 1,021
  • 12
  • 16