Skip to content

2. Batch and Transaction

Peng Ren edited this page Oct 15, 2022 · 2 revisions

Batch Model

With PyDynamoDB, there are three ways to run statements with batch model.

executemany()

sql = """
    INSERT INTO table VALUE {
        'key_partition': ?, 'key_sort': ?, 'col': ?, ...
    }
"""
params = [
    ["test_many_rows_1", 0, ...],
    ["test_many_rows_1", 1, ...],
    ["test_many_rows_1", 2, ...],
    ...
]
cursor.executemany(sql_many_rows_, params_)

autocommit

Once setting autocommit to True, it will trigger to run statements with batch model. (If created multiply cursors within a connection, this way will trigger the batch execution for all cursors) Not allowed to have both read(SELECT) and write(INSERT/UPDATE/DELETE) statements between False and True switch. DDL operations are not supported for batch model also.

with connect() as conn:
    cursor = conn.cursor()
    conn.autocommit = False
    cursor.execute("INSERT INTO ...", [param, ...])
    cursor.execute("UPDATE ...", [param, ...])
    cursor.executemany("INSERT INTO ...", [[param, ...], [param, ...], ...])
    ...
    conn.autocommit = True

flush()

Set autocommit to False, then use flush() to run the statements with batch model for the current cursor.

with connect() as conn:
    cursor = conn.cursor()
    conn.autocommit = False
    cursor.execute("INSERT INTO ...", [param, ...])
    cursor.execute("UPDATE ...", [param, ...])
    cursor.executemany("INSERT INTO ...", [[param, ...], [param, ...], ...])
    ...
    cursor.flush()

Transaction

You are allowed to use begin() and commit() for a block of statements execution for transaction. Not allowed to have both read(SELECT) and write(INSERT/UPDATE/DELETE) statements in one transaction. rollback() is not supported.

begin(), commit()

with connect() as conn:
    conn.begin()
    cursor = conn.cursor()
    cursor.execute("INSERT INTO ...", [param, ...])
    cursor.execute("UPDATE ...", [param, ...])
    cursor.executemany("INSERT INTO ...", [[param, ...], [param, ...], ...])
    conn.commit()

Reference

Please refer to boto3 API:

DynamoDB.Client.batch_execute_statement

DynamoDB.Client.execute_transaction

Clone this wiki locally