myobject = MyObject(id=5)
myobject = Session.merge(myobject)
Session.commit()
will perform the equivalent steps, subject to the transaction isolation in effect.
To lock the row during the SELECT, you can use query.with_lockmode(True).filter(criterion) to SELECT the row with FOR UPDATE, will return None if the row does not exist.
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>
Assuming you're talking about the ORM. You can of course emit the exact SQL you refer to any time using execute() from an engine, connection, or session. The ORM works within a transaction at all times. so an expression like:
myobject = MyObject(id=5)
myobject = Session.merge(myobject)
Session.commit()
will perform the equivalent steps, subject to the transaction isolation in effect.
To lock the row during the SELECT, you can use query.with_lockmode(True).filter(criterion) to SELECT the row with FOR UPDATE, will return None if the row does not exist.
2011/6/15 Michael Bayer <mik...@zzzcomputing.com>Assuming you're talking about the ORM. You can of course emit the exact SQL you refer to any time using execute() from an engine, connection, or session. The ORM works within a transaction at all times. so an expression like:
myobject = MyObject(id=5)
myobject = Session.merge(myobject)
Session.commit()
will perform the equivalent steps, subject to the transaction isolation in effect.I'm not sure this will do the trick as i don't know if the object i'm looking for already exist or not. thus, i don't have it's id ( i don't even know if a range of id is free or not).
To lock the row during the SELECT, you can use query.with_lockmode(True).filter(criterion) to SELECT the row with FOR UPDATE, will return None if the row does not exist.
I'm not sure a lockMode is necessary.
However, I'm far from being an expert or to clearly know the internal mechanisms of DBMS, but reading the Deadlock example in the part 12.3.3 of this page of the postgresql documentation, I thought, that sending a bunch of requests in one transaction would allow to do it as if it was all at once, avoiding any conflicts that could occurs if two almost identical requests were overlapping (like one executing its first statement, the other the second ect..).
> Just curious and learning here -- There are two separate issues here,
> aren't there? (1.) Atomicity of the transaction, taken care of by the
> above discussion, and (2.) what if there was a need to have it be not
> only atomic but consume as little time as possible between the read
> and write, let's say for financial purposes?
Right, so the "executing two statements at once instead of two statements" thing in this kind of case is a fleeting optimization. That is, its a micro optimization that is easily nullified by the surrounding context. Such as, if the way the app works in reality is that the row already exists 95% of the time, the optimization saves negligible time. Or if it's trivial to just pre-insert the rows in question, or a whole selection of 100 rows can be selected at once and just the ones that aren't present can be INSERTed in one multi-row statement, would provide even better performance.
Taking a SELECT then an INSERT and making the choice to turn it into a non-ORM, single statement, database-specific call is something you'd do once the app is up and running, and the separate SELECT/INSERT pair has been observed to be a definite bottleneck with no feasible workaround. I.e. a non-premature optimization.
How can the query/merge or query/add be performed atomically? What would happen if between the two calls another process inserted a row with the same unique key? Wouldn't the `s.merge()` then trigger a `psycopg2.errors.UniqueViolation` exception (in postgres case) when the new row insert is flushed? Is there a way to make the execution of the ORM atomic so that the `s.merge()` would be guaranteed to succeed (barring network errors, etc.)?
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/d117fb88-2fd5-4006-a60f-424cc25c4bd7n%40googlegroups.com.
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.---You received this message because you are subscribed to the Google Groups "sqlalchemy" group.To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/b3002fec-c8ac-4b41-8101-1283455bfe7dn%40googlegroups.com.