twistar icon indicating copy to clipboard operation
twistar copied to clipboard

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

Open xadhoom opened this issue 14 years ago • 11 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...

xadhoom avatar Mar 29 '11 13:03 xadhoom

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<Operation> (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 avatar Mar 30 '11 14:03 xadhoom

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

bmuller avatar Mar 31 '11 12:03 bmuller

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.

xadhoom avatar Apr 29 '11 08:04 xadhoom

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

bmuller avatar Apr 29 '11 18:04 bmuller

Hi,

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

xadhoom avatar May 23 '11 14:05 xadhoom

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

bmuller avatar May 25 '11 15:05 bmuller

whops, sorry, hope all is ok :)

xadhoom avatar May 25 '11 15:05 xadhoom

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!

bmuller avatar May 28 '11 19:05 bmuller

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

xadhoom avatar May 30 '11 11:05 xadhoom

any status updates on this one?

erikkaplun avatar Nov 18 '12 15:11 erikkaplun

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

bmuller avatar Nov 18 '12 17:11 bmuller