CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 64 forks source link

how to use transaction #59

Closed sj82516 closed 6 years ago

sj82516 commented 6 years ago

I would like to apply transaction inside my sql query.

try{
        ctx.conn.beginTransaction()
       ......
} catch (err) {
        logger.error(err)
        ctx.conn.rollback()
    }

However, it throw error "ctx.conn.rollback is not a function" I use ctx.conn.query(...) in many place and works fine. How can I use transaction methods mysql js provided?

Thanks


Or should I directly call connection.query('START TRANSACTION') ..... connection.query('COMMIT')

CodeFoodPixels commented 6 years ago

It should work how you're trying to use it, same as in #34. Without seeing the rest of your code I can't tell you why it's not working.

sj82516 commented 6 years ago

Thanks for your rapid reply! When I replace conn. beginTransaction() to conn.query('START TRANSACTION'), it works properly. I can't find out where goes wrong.

CodeFoodPixels commented 6 years ago

Just found something. You'll need to get a connection from the pool using .getConnection() before doing the transaction using that connection. Have a look in the readme for an example.

sj82516 commented 6 years ago

It works. Thanks a lot. please close this issue