471,348 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

SQLAlchemy, flush vs. transaction

6
I have the following piece of code:
Expand|Select|Wrap|Line Numbers
  1. transaction = session.create_transaction()
  2. try:
  3.     # Do some work here that might fail
  4.     p=P()
  5.     p.ID=333
  6.     session.save(p)
  7.     session.flush()
  8.     # Do some more work here that might fail
  9.     p1=P()
  10.     p1.ID=333
  11.     session.save(p1)
  12.     session.flush()
  13.     # Success, commit everything
  14.     transaction.commit()
  15. except:
  16.     # Make sure the transaction is rolled back ...
  17.     transaction.rollback()
  18.     # ... then propagate the error upwards to be handled elsewhere
  19.     raise
The 2nd flush fails (because there is already an ID - 333) and then rollback is called, but the first row entered remains in the database.
Wasn't rollback supposed to remove it?
Jun 25 '07 #1
6 6463
bartonc
6,596 Expert 4TB
I have the following piece of code:
Expand|Select|Wrap|Line Numbers
  1. transaction = session.create_transaction()
  2. try:
  3.     # Do some work here that might fail
  4.     p=P()
  5.     p.ID=333
  6.     session.save(p)
  7.     session.flush()
  8.     # Do some more work here that might fail
  9.     p1=P()
  10.     p1.ID=333
  11.     session.save(p1)
  12.     session.flush()
  13.     # Success, commit everything
  14.     transaction.commit()
  15. except:
  16.     # Make sure the transaction is rolled back ...
  17.     transaction.rollback()
  18.     # ... then propagate the error upwards to be handled elsewhere
  19.     raise
The 2nd flush fails (because there is already an ID - 333) and then rollback is called, but the first row entered remains in the database.
Wasn't rollback supposed to remove it?
Looks like you're leaving out the transaction.begin() call.
I'm not familiar with that RDBM, but these docs look very well done.
Jun 25 '07 #2
mcmc
6
SessionTransaction do not have a begin() method.
None of the examples have it.
See http://www.sqlalchemy.org/docs/tutorial.html#tutorial_orm_transactions
Jun 26 '07 #3
bartonc
6,596 Expert 4TB
SessionTransaction do not have a begin() method.
None of the examples have it.
See http://www.sqlalchemy.org/docs/tutorial.html#tutorial_orm_transactions
Connections do, though:
The Connection object provides a begin() method which returns a Transaction object. This object is usually used within a try/except clause so that it is guaranteed to rollback() or commit():
Jun 27 '07 #4
mcmc
6
OK, but the question was about the code that I posted. Why doesn't it work?
Jun 28 '07 #5
mcmc
6
Found the problem.
I needed to use the InnoDB engine.
Jun 28 '07 #6
bartonc
6,596 Expert 4TB
Found the problem.
I needed to use the InnoDB engine.
Nice! Thanks for the update.
Jun 28 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

20 posts views Thread by John Salerno | last post: by
1 post views Thread by tomec | last post: by
1 post views Thread by Karlo Lozovina | last post: by
reply views Thread by Karlo Lozovina | last post: by
6 posts views Thread by Ilias Lazaridis | last post: by
3 posts views Thread by Greg Copeland | last post: by
reply views Thread by Nathan Harmston | last post: by
3 posts views Thread by mcmc | last post: by
2 posts views Thread by Adam Kubica | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.