-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
docs: clarify how sqlalchemy uses SQL transactions and how to combine with client retries #26966
Comments
@nstewart @awoods187 Please provide example code that reproduces the error |
@awoods187 I'm sorry this issue is currently either improperly titled or lacking description. The sentence "ORM expect savepoints to be the first statement in a transaction" is factually false - the reason why you observe an error is instead that your program is trying to use a non-savepoint statement first. We can't really do anything with this issue as-is. Please provide more information. |
Please give us time to produce the “sample code”. We saw your first note
On Fri, Jun 29, 2018 at 9:57 PM kena ***@***.***> wrote:
@awoods187 <https://github.com/awoods187> I'm sorry this issue is
currently either improperly titled or lacking description. The sentence
"ORM expect savepoints to be the first statement in a transaction" is
factually false - the reason why you observe an error is instead that your
program is trying to use a non-savepoint statement first.
We can't really do anything with this issue as-is. Please provide more
information.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#26966 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA2lMSAJNHSyCmBUjZhwYWMcimXuzHcBks5uBtsngaJpZM4U25l7>
.
--
Nate Stewart
Head of Product, Cockroach Labs
Check out our blog: www.cockroachlabs.com/blog/
|
SQLAlchemy + Postgres output
SQLAlchemy + CockroachDB output
The repro code can be found here. |
The sqlalchemy code is missing a txn begin call, so it is attempting to put all the SQL queries into a single (implicit) transaction. print_account_balances()
run_transaction(session, lambda conn: transfer_funds(session, 1, 2, 100))
print_account_balances() try this: print_account_balances()
session.begin()
run_transaction(session, lambda conn: transfer_funds(session, 1, 2, 100))
print_account_balances() Arguably our doc that explains how to use |
It's explained here: http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html |
@rmloveland if you could have a look at this that would be swell. thanks! |
@rmloveland also as Andy suspects, this problem may not be specific to SQLAlchemy. I think we should call out in more places that any client-side library or framework is more likely than not to start transactions implicitly, the client-directed retry system does not support this (well), so the two must be combined with care (making txns explicit). |
There are two things here:
|
SQLAlchemy's session model has never made much sense to me. I believe the best way to use it is to create a new session for each transaction (if you don't, it may have implicit caches that obscure what's going on and create transactional inconsistencies). Our official run_transaction wrapper takes a sessionmaker instead of a session for this reason. (You should be using this instead of writing your own, BTW. We need to document this) If you do want to reuse sessions, you must commit the implicit transaction before starting a new transaction using savepoints. In |
Thanks @bdarnell that works. While I have you, can you help me understand if general savepoint support would make this work out of the box (like the above demo app does when connected to pg)? In your blog you say Would adding that alleviate this whole class of ORM problems? |
Generalized savepoints are a useful feature that some ORMs can take advantage of, but it doesn't exactly help with the current issue. The special
To the extent that we are able to recommend tools instead of meeting users with the tools they are already using, we should promote low-magic tools like the SQLAlchemy query builder instead of its ORM and Sessions (and analogous choices in other languages, such as jOOQ instead of Hibernate for java). |
Closing since this was addressed via docs issue #3315. |
sqlalchemy.exc.InternalError: (psycopg2.InternalError) SAVEPOINT COCKROACH_RESTART needs to be the first statement in a transaction
Ran into on the MOVR database cockroachdb/movr#16
The text was updated successfully, but these errors were encountered: