bmuller / twistar

Twistar is an object-relational mapper (ORM) for Python that uses the Twisted library to provide asynchronous DB interaction.

Home Page:http://findingscience.com/twistar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

xadhoom opened this issue · comments

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

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?

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

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.

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

Hi,

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

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

whops, sorry, hope all is ok :)

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!

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 :)

any status updates on this one?

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