Getting benifits of database transactions in an OO way?

L

Leif K-Brooks

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).
 
P

Paul Rubin

Leif K-Brooks said:
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()
 
L

Leif K-Brooks

Paul said:
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()

How woould begin_transaction() and finish_transaction() be implemented?
They couldn't be simple wrappers for PostgreSQL transaction handling,
since it's not very object-oriented (the whole connection has one
transaction at a time).
 
P

Paul Rubin

Leif K-Brooks said:
How woould begin_transaction() and finish_transaction() be
implemented? They couldn't be simple wrappers for PostgreSQL
transaction handling, since it's not very object-oriented (the whole
connection has one transaction at a time).

Yes, if you want multiple concurrent transactions, you need separate
connections for them.
 
L

Leif K-Brooks

Paul said:
Yes, if you want multiple concurrent transactions, you need separate
connections for them.

But then multiple row classes would have to create their own
connections, which would be bad for performance:

import people
import companies
people_transaction = people.begin_transaction()
companies_transaction = companies.begin_transaction()
microsoft = companies_transaction.find_by_name('Microsoft')
microsoft.patent_something_trivial()
bill = people_transaction.find_by_name("Bill Gates")
bill.money += 1000000
people_transaction.finish_transaction()
companies_transaction.finish_transaction()

I think I'll just require users of modules which handle data to commit
on their own. Not ideal, but it seems to be the best possible.
 
P

Paul Rubin

Leif K-Brooks said:
But then multiple row classes would have to create their own
connections, which would be bad for performance:

import people
import companies
people_transaction = people.begin_transaction()
companies_transaction = companies.begin_transaction()

You wouldn't do it that way.
I think I'll just require users of modules which handle data to commit
on their own. Not ideal, but it seems to be the best possible.

You could take a look at how javabeans does it.
 
L

Leif K-Brooks

Paul said:
You wouldn't do it that way.

How would I do it? If you have the time, I'd really appreciate a
simplified example of how you'd write the "people" module.
You could take a look at how javabeans does it.

I've tried Googling for "javabeans", but it's kind of like Googling for
the word "the". Is there an example of Javabeans you would recommend
looking at?
 
J

Jeffrey Froman

Leif said:
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.

You might want to take a look at http://sqlobject.org (transaction examples
provided in the documentation as well.)

Jeffrey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top