473,412 Members | 5,714 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,412 software developers and data experts.

Getting benifits of database transactions in an OO way?

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).
Jul 18 '05 #1
8 2030
Leif K-Brooks <eu*****@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()
Jul 18 '05 #2
Paul Rubin wrote:
Leif K-Brooks <eu*****@ecritters.biz> writes:
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()


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).
Jul 18 '05 #3
Leif K-Brooks <eu*****@ecritters.biz> writes:
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.
Jul 18 '05 #4
Paul Rubin wrote:
Leif K-Brooks <eu*****@ecritters.biz> writes:
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.


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.
Jul 18 '05 #5
Leif K-Brooks <eu*****@ecritters.biz> writes:
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.
Jul 18 '05 #6
Paul Rubin wrote:
Leif K-Brooks <eu*****@ecritters.biz> writes:
import people
import companies
people_transaction = people.begin_transaction()
companies_transaction = companies.begin_transaction()

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?
Jul 18 '05 #7
Leif K-Brooks wrote:
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
Jul 18 '05 #8
Jeffrey Froman wrote:
You might want to take a look at http://sqlobject.org (transaction examples
provided in the documentation as well.)


Wow, nice. Thanks a lot for the link.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marcus | last post by:
Hello, Say I have a function with 5 MySQL queries that all insert or update the database. Now let's assume that during execution, the first 3 queries are executed fine but then the 4th one dies...
16
by: noah | last post by:
Does PHP have a feature to associate Cookie sessions with a persistent database connection that will allow a single transaction across multiple HTTP requests? Here is how I imagine my process: I...
4
by: DBA | last post by:
If I detach a database on a server, then attach the database with a single file only using the data file...will I lose any transactions that were in the original log file? There is a debate going...
13
by: Lew | last post by:
Hi, I am a bit confused about what I'm seeing on my systems. I am trying to develop a script that will determine what percentage of the total log space is being used. If it is too high (80%)...
3
by: Peter Kirk | last post by:
Hi there when working with database connections in C# is there a concept of "auto commit"? For instance, if I obtain a database connection in java I can set the auto-commit state to true or...
1
by: bob1barker | last post by:
we are creating a database of sales agents. Basically I have a table of about 35,000 people, a second one with 8000 offices, and a third table of around 400,000 transactions done by those 35,000...
10
by: giraffeboy | last post by:
Hi there, I'm having a problem with the Python db api, using MySQL. I've written a program with a GUI using wxPython, the GUI is contained in main.py which imports another module - reports.py....
3
by: Michael Schöller | last post by:
Hello, First of all english is not my natural language so please fogive me some bad mistakes in gramatic and use of some vocables :). I have a great problem here. Well I will not use it...
5
by: Roger | last post by:
backup log testdb with truncate_only DBCC SHRINKFILE (testdb_log, 100) WITH NO_INFOMSGS backup database testdb to disk = '\\DC01\Backups\DB01\testdb.bak' with init and does the shrinkfile...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.