Getting benifits of database transactions in an OO way?

Leif K-Brooks eurleif at
Sun Aug 22 02:51:12 CEST 2004

I'm coding an application which makes extensive use of a PostgreSQL 
database. To make code clearer, I'm wrapping various row types in 
objects. Sample code would look something like this:

import people
fred = people.find_by_name('Fred Flintstone')
barney = people.find_by_name('Barney Rubble') -= 10 += 10

Right now, the Person.save_data method also commits the current database 
transaction. But that seems to remove the benifit of having a database 
with transactions: If something dies between the call to 
fred.save_data() and barney.save_data(), Fred's $10 will end up in a 
black hole somewhere.

Is the only option making Person.save_data not commit the transaction, 
and require calling code do so on its own? I don't really like the idea, 
since it removes the abstraction which allows the Person class to save 
data anywhere it chooses (not just in a database).

More information about the Python-list mailing list