473,414 Members | 1,677 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,414 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 6590
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.