transaction.atomic() crashes if connection isn't initialized

XMLWordPrintableJSON

    • 🔵 Done
    • Python Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Reported on GitHub PR 412: 

      I ran into a problem during a transaction when I call the POST create method in the view and try to create documents through the serializer:

      class CreateTariff(TariffsSerializer):
          @transaction.atomic(using="mongodb")
          def create(self, validated_data):
              ...
      

      This error occurs spontaneously and always at the first call to the endpoints API.

      File: C\test\venv\lib\site-packages\rest_framework\views.py in dispatch (line 506)
      response = handler(request, *args, **kwargs)
      
      File: C:\test\venv\lib\site-packages\rest_framework\mixins.py in create (line 19)
      self.perform_create(serializer)
      
      File: C:\test\venv\lib\site-packages\rest_framework\mixins.py in perform_create (line 24)
      serializer.save()
      
      File: C:\test\venv\lib\site-packages\rest_framework\serializers.py in save (line 208)
      self.instance = self.create(validated_data)
      
      ...
      
      File: C:\test\venv\lib\site-packages\django_mongodb_backend\transaction.py in __enter__ (line 29)
      connection.start_transaction_mongo()
      
      File: C:\test\venv\lib\site-packages\django\utils\asyncio.py in inner (line 26)
      return func(*args, **kwargs)
      
      File: C:\test\venv\lib\site-packages\django_mongodb_backend\base.py in start_transaction_mongo (line 275)
      self.session = self.connection.start_session()
      Error: 'NoneType' object has no attribute 'start_session'
      

      As I understand it, it occurs due to the fact that the connection lazily creates a connection (connection.connection) during the entry into the transaction.

            Assignee:
            Tim Graham
            Reporter:
            Tim Graham
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: