Skip to content

Better handling of committing nested transactions #3725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions src/zenml/zen_stores/sql_zen_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2819,7 +2819,7 @@ def _get_or_create_artifact_for_name(

if artifact is None:
try:
with session.begin_nested():
with session.begin_nested() as nested_session:
artifact_request = ArtifactRequest(
name=name,
project=project_id,
Expand All @@ -2830,16 +2830,14 @@ def _get_or_create_artifact_for_name(
)
artifact = ArtifactSchema.from_request(artifact_request)
session.add(artifact)
session.commit()
session.refresh(artifact)
nested_session.commit()
except IntegrityError:
# We have to rollback the failed session first in order to
# continue using it
session.rollback()
# We failed to create the artifact due to the unique constraint
# for artifact names -> The artifact was already created, we can
# just fetch it from the DB now
artifact = session.exec(artifact_query).one()
else:
session.refresh(artifact)

if artifact.has_custom_name is False and has_custom_name:
# If a new version with custom name was created for an artifact
Expand Down Expand Up @@ -12003,17 +12001,15 @@ def _create_tag_schema(
validate_name(tag)
self._set_request_user_id(request_model=tag, session=session)

with session.begin_nested() as nested_session:
tag_schema = TagSchema.from_request(tag)
session.add(tag_schema)

try:
session.commit()
except IntegrityError:
nested_session.rollback()
raise EntityExistsError(
f"Tag with name `{tag.name}` already exists."
)
try:
with session.begin_nested() as nested_session:
tag_schema = TagSchema.from_request(tag)
session.add(tag_schema)
nested_session.commit()
except IntegrityError:
raise EntityExistsError(
f"Tag with name `{tag.name}` already exists."
)
return tag_schema

@track_decorator(AnalyticsEvent.CREATED_TAG)
Expand Down
Loading