0

I've searched already on the internet, but couldn't find a solution to my problem. I'm using NodeJS and a MariaDB with a pool connection. I get a connection from the pool, make a transaction (no errors) but I cannot see any chances inside my database. If I use the query without a transaction, just with pool.query(...) then it works fine. I know for just one query I wouldn't need a transaction, but I have just simplified the code for you.

pool.getConnection()
.then(conn =>{
    conn.beginTransaction()
        .then(() =>{
            return conn.query("UPDATE Users SET forename='Tom' WHERE user_id=8")
        })
        .then(()=>{
            console.log("commit")
            conn.commit()
            //conn.end() --> doesn't change anything

        })
        .catch((err)=>{
            console.log(err.message);
            conn.rollback()
    })
});

What's wrong here?

Thanks!

5
  • Does it work without using a pool? Commented Nov 14, 2018 at 13:29
  • no, doesn't work either:/ Commented Nov 14, 2018 at 13:32
  • only thing working was creating a new connection for the query, but this seems strange to me Commented Nov 14, 2018 at 13:35
  • I agree. I assume that nothing is being logged by MariaDB either? Perhaps this issue is relevant: github.com/MariaDB/mariadb-connector-nodejs/issues/… (so instead of conn.commit() you should try and use conn.query('COMMIT')). And FWIW, I think that you still need to call conn.end() when you're done with the transaction, to release the connection back to the pool. Commented Nov 14, 2018 at 13:41
  • using conn.query("commit") fixed it:) Commented Nov 14, 2018 at 13:47

2 Answers 2

1

Looks like this is a bug in the MariaDB driver, where conn.commit() doesn't actually commit. The bug is documented here.

In that comment, a workaround is suggested, by calling COMMIT manually:

conn.query('COMMIT');

Also make sure that you end the connection when you're done, to release the connection back into the pool:

.then(()=>{
  return conn.query("COMMIT").then(() => {
    return conn.end();
  });
})
.catch((err)=>{
  return conn.query("ROLLBACK").then(() => {
    conn.end();
    throw new Error(err)
  });
})
Sign up to request clarification or add additional context in comments.

6 Comments

yes that worked:) Where should I close the connections? Is this right?
does it also work like I did? Or is it neccessary to return conn.end()?
@mcAngular2 it may work with your solution, but it would depend on how the MariaDB library is implemented. The reason why I use return conn.* is that that will cause the promises that are returned by each operation to be resolved before the next .then is executed, whereas using conn.query(...); conn.end() doesn't do that.
but how can I then know whether the connection was closed because of conn.rollback or conn.commit? I need to know whether everything worked or not
btw, this is jira.mariadb.org/browse/CONJS-52, and will be corrected in next release (in a day)
|
1

This is a bug, and as @robertklep indicate, a workaround is to execute a conn.query("COMMIT") command.

But is jira.mariadb.org/browse/CONJS-52, and is now corrected with the latest release (2.0.1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.