📖
Docs
  • Hi there 👋
  • Python
    • Tutorial
      • Class
      • Context Managers
      • Iterators and Iterables and generators
      • Lambda Operator
      • Decorators
      • Lập trình đa luồng
      • Singleton
      • Logging
      • Best practices
    • Django
      • Lazy queryset
      • Sql injection
      • Transaction
    • Flask
    • Fastapi
  • Struct data and algorithms
    • Struct data
    • Algorithms
  • database
    • Nosql và RDBMS
    • Index sql
    • Inverted Index
    • Migrate database best
    • Datatype
  • Cache
    • Caching strategies
    • Cache replacement policies
  • Message queue
    • Message queue
  • Other
    • Clean code
    • Design pattern
    • Encode-decode
    • Security
    • Docker
    • Celery
  • deploy
    • Jenkins
Powered by GitBook
On this page
  1. Python
  2. Django

Transaction

  • Autocommit (default): Mỗi truy vấn được cam kết ngay lập tức với cơ sở dữ liệu, trừ khi một giao dịch đang hoạt động

  • atomic

    @action(methods=['GET'], detail=False, url_path='test')
    @transaction.atomic
    def test(self, request, *args, **kwargs):
        Book.objects.create(name=1)
        try:
            with transaction.atomic():
                Book.objects.create(name=2)
                print(1/0)
        except:
            pass
        Book.objects.create(name=3)
        return Response()

--> Đoạn code save thành công book1 và book3 trong database.

  • on_commit: Thực hiện một hành động liên quan đến giao dịch cơ sở dữ liệu hiện tại, nhưng chỉ khi giao dịch được thực hiện thành công

    # celery task
    transaction.on_commit(lambda: my_fav_task.delay(param1))
cursor = connection.cursor()
cursor.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE')
    +  READ COMMITTED: read data đã commit
    +  READ UNCOMMITTED: read data chưa commit
    +  REPEATABLE READ: read data không thay đổi trong 1 transaction
    +  SERIALIZABLE: lock read và write
PreviousSql injectionNextFlask

Last updated 2 years ago