Getting benifits of database transactions in an OO way?

Paul Rubin http
Sat Aug 21 21:20:53 EDT 2004


Leif K-Brooks <eurleif at ecritters.biz> writes:
> 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.

You have to use transactions for that.  E.g.

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



More information about the Python-list mailing list