473,240 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 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 6586
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

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

Similar topics

20
by: John Salerno | last post by:
Are there any major differences between these two? It seems they can both be used with TurboGears, and SQLAlchemy with Django. I'm just wondering what everyone's preference is, and why, and if...
1
by: tomec | last post by:
hello, Ive read some articles about SqlAlchemy but i don't know where i can use this? Lets say that i would like to create some application which will be using sqlAlchemy My question is which...
1
by: Karlo Lozovina | last post by:
I've installed SQLAlchemy under Windows (strangely, it didn't install inside ../site-packages/ as a directory, but rather as a SQLAlchemy-0.2.8-py2.4.egg file). I can import it with 'import...
0
by: Karlo Lozovina | last post by:
I've just upgraded to Python 2.5, SQLAlchemy 0.3.3, and py2exe 0.6.5 (the py2.5 version, yes). Simple: --- import sqlalchemy print 'Test' ---
6
by: Ilias Lazaridis | last post by:
- / metaperl: Ilias Lazaridis: Michael Bayer wrote within: http://groups.google.com/group/sqlalchemy/msg/9d7a096a61abfc6f I understand your elaborations.
3
by: Greg Copeland | last post by:
I'm using SQLAlchemy and have a need to call an Oracle function; which is not the same as a stored procedure. Can this be done directory or indirectly with SQLAlchemy? If so, can someone please...
0
by: Nathan Harmston | last post by:
HI, I posted this to sqlalchemy but didnt get a response, so I was wondering if anyone on python-list could help me. I m currently trying to build an api for a database and I really like the...
3
by: mcmc | last post by:
Hi, I need to lock a table for WRITE. The scenario is that I have a SELECT followed by an INSERT, where the INSERT depends on the outcome of the SELECT. I have to make sure that no other process is...
2
by: Adam Kubica | last post by:
Hello. I have pylons 0.96 (SVN) and current SQLAlchemy (0.3.10), and I have bug that doesn't exist earlier. My connection code: <code> import sqlalchemy.mods.threadlocal from sqlalchemy...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.