Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Idea on how to encapsulate many ORM operations into a transaction ? #9

Open
xadhoom opened this issue Mar 29, 2011 · 11 comments
Open

Comments

@xadhoom
Copy link
Contributor

xadhoom commented Mar 29, 2011

I'm thinking a way to implement a transaction handling to protect several operations,
somethink like AR does:

Account.transaction do
balance.save!
account.save!
end

(see http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html for other examples).

this is very useful when you want to do several operations with the orm and rollback if any of these operations fails. The rollback can be automatic (like twisted txns do) and an exception can be raised back to the application.

what do you suggest can be the best way to implement into twistar ?
I can start looking at it...

@xadhoom
Copy link
Contributor Author

xadhoom commented Mar 30, 2011

after looking into the code, I have a possible idea:

  • add a method into dbobject.py which returns a t.e.a.Transaction and sets it as a DBObject.txn attribute. the method could be named startTxn(). The code can be similar to t.e.a.ConnectionPool._runInteraction and being implemented into dbconfig/base.py
  • modify base.py entry points (updateObj, insertObj, refreshObj, delete) to check the presence of the DBObject.txn attribute, if is not None just call the correspondent private functions _do (for example _doupdate) instead of calling Registry.DBPOOL.runInteraction.
  • add another method to DBObject which commits the transaction by calling DBObject.txn.commit
  • add another method to DBObject which rollbacks the transaction by calling DBObject.txn.rollback

of course is mandated to the library user to call DBObject.startTxn() and commit/rollback and the end of ORM operations.

what do you think? can be a viable idea?

@xadhoom xadhoom closed this as completed Mar 30, 2011
@bmuller bmuller reopened this Mar 31, 2011
@bmuller
Copy link
Owner

bmuller commented Mar 31, 2011

Without the support of Ruby's codeblocks, I think your suggestion is the best idea. If you want to start something, I can take a look.

Thanks!

b

@xadhoom
Copy link
Contributor Author

xadhoom commented Apr 29, 2011

Hi!
I've implemented transaction support in my branch https://github.com/xadhoom/twistar, maybe you want to take a look at it, if you have any comments.

Also tests has been included.

@bmuller
Copy link
Owner

bmuller commented Apr 29, 2011

Excellent - thanks. I'll take a look this weekend.

@xadhoom
Copy link
Contributor Author

xadhoom commented May 23, 2011

Hi,

did you had time to check the implementation? what do you think about it?

@bmuller
Copy link
Owner

bmuller commented May 25, 2011

Sorry - been slammed - will try to look this week.

@xadhoom
Copy link
Contributor Author

xadhoom commented May 25, 2011

whops, sorry, hope all is ok :)

@bmuller
Copy link
Owner

bmuller commented May 28, 2011

I looked through your changes - other than the one line note, I think everything looks good.

Would you be willing to write some basic docs for transactions? If so, you can just make a transactions.xhml file in the docs folder (using lore).

Thanks again!

@xadhoom
Copy link
Contributor Author

xadhoom commented May 30, 2011

ok, I've fixed the one line code and added some documentation.
(you may want to read it, my english is far from being perfect).

if you like it, please merge it :)

@erikkaplun
Copy link
Contributor

any status updates on this one?

@bmuller
Copy link
Owner

bmuller commented Nov 18, 2012

This one needs a code review with some modifications necessary - I just haven't had a chance to go through it yet.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants