Skip to content

Conversation

@jrcastro2
Copy link
Contributor

No description provided.

@jrcastro2 jrcastro2 force-pushed the add-rollback branch 5 times, most recently from 46d9b1d to bf11b8f Compare December 2, 2024 16:32

with db.session.begin_nested():
_id = create_user(invenio_user)
db.session.flush()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be needed? the begin_nested will commit the transaction on_exit

Suggested change
db.session.flush()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it should flush before entering and on exit, so we should be safe, thanks 👍

db.session.flush()
inserted.add(_id)

db.session.commit()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each begin_nested should already commit each time

Suggested change
db.session.commit()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, If I understand correctly the begin_nested does "commit" the SAVEPOINT meaning that it flushes those changes and does release the SAVEPOINT but doesn´t do the actual commit to the DB until we call db.session.commit()

When the context manager yielded by Session.begin_nested() completes, it “commits” the savepoint, which includes the usual behavior of flushing all pending state.

When the .commit() method on this object is called, “RELEASE SAVEPOINT” is emitted to the database, and if instead the .rollback() method is called, “ROLLBACK TO SAVEPOINT” is emitted. The enclosing database transaction remains in progress.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it is only "closing" the transaction/savepoint.
However, we are now basically isolating the transaction, and potentially rollback. If one fails, the other will still work and succeed with a single db.session.commit() at the end.
I think we should find a tradeoff: what about having a commit after e.g. 1000 users?

@jrcastro2 jrcastro2 merged commit 94f4f08 into CERNDocumentServer:main Dec 3, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants