Skip to content

Commit

Permalink
Update transaction context manager to default write_on_commit to …
Browse files Browse the repository at this point in the history
…`True` (#15515)
  • Loading branch information
desertaxle authored Sep 30, 2024
1 parent bc43e28 commit 7f8e852
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
7 changes: 3 additions & 4 deletions docs/3.0/develop/transactions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def write_data(data: str):

@flow(log_prints=True)
def pipeline():
with transaction(key="download-and-write-data", write_on_commit=True) as txn:
with transaction(key="download-and-write-data") as txn:
if txn.is_committed():
print("Data file has already been written. Exiting early.")
return
Expand All @@ -168,7 +168,7 @@ if __name__ == "__main__":

If you run this flow, it will write data to a file the first time, but it will exit early on subsequent runs because the transaction has already been committed.

Giving the transaction a `key` and setting `write_on_commit=True` will cause the transaction to write a record on commit signifying that the transaction has completed.
Giving the transaction a `key` will cause the transaction to write a record on commit signifying that the transaction has completed.
The call to `txn.is_committed()` will return `True` only if the persisted record exists.

### Handling race conditions
Expand Down Expand Up @@ -202,7 +202,7 @@ def write_file(contents: str):

@flow
def pipeline(transaction_key: str):
with transaction(key=transaction_key, write_on_commit=True) as txn:
with transaction(key=transaction_key) as txn:
if txn.is_committed():
print("Data file has already been written. Exiting early.")
return
Expand Down Expand Up @@ -257,7 +257,6 @@ def write_file(contents: str):
def pipeline(transaction_key: str):
with transaction(
key=transaction_key,
write_on_commit=True,
isolation_level=IsolationLevel.SERIALIZABLE,
store=ResultStore(
lock_manager=FileSystemLockManager(
Expand Down
7 changes: 2 additions & 5 deletions src/prefect/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
ResultRecord,
ResultStore,
get_result_store,
should_persist_result,
)
from prefect.utilities.annotations import NotSet
from prefect.utilities.collections import AutoEnum
Expand Down Expand Up @@ -438,7 +437,7 @@ def transaction(
commit_mode: Optional[CommitMode] = None,
isolation_level: Optional[IsolationLevel] = None,
overwrite: bool = False,
write_on_commit: Optional[bool] = None,
write_on_commit: bool = True,
logger: Union[logging.Logger, logging.LoggerAdapter, None] = None,
) -> Generator[Transaction, None, None]:
"""
Expand Down Expand Up @@ -473,9 +472,7 @@ def transaction(
commit_mode=commit_mode,
isolation_level=isolation_level,
overwrite=overwrite,
write_on_commit=write_on_commit
if write_on_commit is not None
else should_persist_result(),
write_on_commit=write_on_commit,
logger=logger,
) as txn:
yield txn

0 comments on commit 7f8e852

Please sign in to comment.