Getting benifits of database transactions in an OO way?

Leif K-Brooks eurleif at ecritters.biz
Sat Aug 21 20:51:12 EDT 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')
fred.money -= 10
barney.money += 10
fred.save_data()
barney.save_data()

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