Transaction example

738 views
Skip to first unread message

Hugo Magalhaes

unread,
Jan 21, 2013, 11:02:34 AM1/21/13
to node-t...@googlegroups.com
Hi,
I'm trying to use transactions and although I found some info in the documentation, I'm struggling on how to do this due to the asynchronous nature of nodejs.
Can anyone please help me by showing an example?

Thanks.
Best regards,
Hugo

Mike Pilsbury

unread,
Jan 21, 2013, 6:42:30 PM1/21/13
to node-t...@googlegroups.com
https://github.com/pekim/tedious/blob/master/test/integration/transactions-test.coffee includes some tests, that show one approach. (Sorry it's in coffeescript.)

Connection's beginTransaction and commitTransaction (and of course rollbackTransaction) work similarly to the execSql and execSql functions. They all perform an operation on the connection asynchronously, and call a callback when they have completed (or failed).

In the simplest scenario:
  • call connection.beginTransaction
    • in it's callback, call connection.execSql
      • in it's callback, call connection.commitTransaction
This assumes all calls complete without errors, and you always want to commit (never rollback).

As in any node code with lots of asynchronous requests, you may want to use a helper library such as async (or you may be more comfortable staying away from them).

Mike

Hugo Magalhaes

unread,
Jan 22, 2013, 6:03:44 AM1/22/13
to node-t...@googlegroups.com
Hi Mike,

Thanks for the help!
I already use async. (-:

Best regards,
Hugo
Reply all
Reply to author
Forward
0 new messages