S
S
Sergey Eremin2020-12-12 00:27:52
MySQL
Sergey Eremin, 2020-12-12 00:27:52

Why doesn't "with transaction.atomic()" work in django when writing to several different databases?

I'm moving data from one (remote) database to a local one. Remote -- "old_z". Something like this code:

q_ext_orders = TbExtOrder.objects.using("old_z").filter(bExported=False)
order_error = 0
for ext_order in q_ext_orders:
    try:
        with transaction.atomic():
            int_order_archive = TbOrderArchive(
                szOrderCheck=ext_order.szOrderCheck,
                iAccount=ext_order.iAccount,
                iPay=ext_order.iPay
                # и еще куча полей
            )
            int_order_archive.save()
            ext_order.bExported = True
            ext_order.save(using="old_z")
            print_log(msg="IMPORT ORDER %s" % (ext_order.szOrderCheck,), status="OK")
    except DatabaseError:
        ext_order.bExported = False
        order_error += 1
        print_log(msg="IMPORT ORDER %s" % (ext_order.szOrderCheck,), status="ERR1")
    except IntegrityError:
        ext_order.bExported = False
        order_error += 1
        print_log(msg="IMPORT ORDER %s" % (ext_order.szOrderCheck,), status="ERR2")

All this is wrapped in cycles, and more in cycles... and there are checks that ... But when the code is forced to end (process restart, for example), writing to the local database may not occur. Those. for a remote database, a piece of code:order_error != 0

ext_order.bExported = True
ext_order.save(using="old_z")
performed. And in the local database, a piece of code:
int_order_archive = TbOrderArchive(
    szOrderCheck=ext_order.szOrderCheck,
    iAccount=ext_order.iAccount,
    iPay=ext_order.iPay
    # и еще куча полей
)
int_order_archive.save()
is not executed... At the same time, as we see, both entries ( and ) are inside ! Moreover, it is in order: first write to the local database, and then write to the remote one !! But not only does the transaction not work and exceptions are not handled, it also writes to the remote database, but not to the local one! Those. the piece of code written above does not work, but the code below it works! Has anyone experienced this? PS I tried to execute both in the main Django process, and in parallel under django-backgrond-tasks - the result is the same. Those. the result is not predictable. Maybe after the resumption of work it is correct to register in both databases, or maybe not. ext_order.save(using="old_z")int_order_archive.save()with transaction.atomic():


Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-12-12
@Sergei_Erjemin

Weird question. Obviously, transactions exist within the same database. You have a local database transaction opened in a context block and, of course, when the code is forced to complete, it is rolled back, and operations with the old_z database are performed without a transaction, so there is no rollback.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question