-
Notifications
You must be signed in to change notification settings - Fork 4
2. Batch and Transaction
With PyDynamoDB, there are three ways to run statements with batch model.
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_)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 = TrueSet 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()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.
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()Please refer to boto3 API: