-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Option required to start TRANSACTION #1047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi! You should be able to write your function executeQuery(query, readQuery) {
var performQuery = readQuery ? runQuery : runTransactQuery;
var pool = readQuery ? 'READ' : 'READ-WRITE';
poolCluster.getConnection(pool, 'RANDOM', function(err, conn) {
if (err) throw err; // always handle your errors in some way
performQuery(conn, query, function (err) {
conn.release();
if (err) throw err; // always handle your errors in some way
});
});
}
function runQuery(conn, query, callback) {
conn.query(query, callback);
}
// mostly take from https://github.com/felixge/node-mysql#transactions
function runTransactQuery(conn, query, callback) {
conn.beginTransaction(function (err) {
if (err) return callback(err);
conn.query(query, rollbackOnError(conn, callback, function (err) {
if (err) return callback(err);
conn.commit(rollbackOnError(conn, callback, callback));
}));
});
}
function rollbackOnError(conn, callback, fn) {
return function (err) {
if (!err) return fn.apply(this, arguments);
conn.rollback(function () {
callback(err);
});
};
} |
Thanks @dougwilson for reply. But sorry to say, when I have write code as you suggest, it's not work. |
Unfortunately there isn't any more information you provided that I can work with. For example, what does it mean to not work? Are any of those error variables populated? In your original example, you were not checking your error variable, and if you don't, then you cannot expect threadId to work. |
Hi @dougwilson, In my question, I have wrote code as follows : function executeQuery(query, readQuery ) {
poolCluster.of((readQuery ? "READ" : "READ-WRITE"), 'RANDOM').getConnection(
function(err, connection) {
if (err) {
//log error here
return;
else if (connection) {
console.log('connected id ' + connection.threadId); //Here I am not getting threadId, why ?????
connection.query(query, function(err, rows, fields) {
connection.release(); //send back to pool
if (err) { throw err; }
elseif (rows && rows.length > 1) {
callback(null, rows);
}
});
}
}) and it's working fine. but when I have changed my code as you mention then rows.length is showing undefined and my
and for threadId, If err on null/undefined, still "threadId" is undefined/null. my last code is as follows (As per your example) function executeQuery(query, readQuery ) {
poolCluster.of((readQuery ? "READ" : "READ-WRITE"), 'RANDOM').getConnection(
function(err, connection) {
if (err) {
//log error here
return;
else if (connection) {
console.log('connected id ' + connection.threadId); //Here I am not getting threadId, why ?????
var performQuery = readonly ? runQuery : runTransactQuery;
performQuery(connection, query, function(err, rows, fields) {
console.log('result ' + err + ', rows:' + JSON.stringify(rows) + ', len:' + rows.length);
connection.release(); //send back to pool
if (err) { return callback(err, null); }
elseif (rows && rows.length > 1) {
callback(null, rows);
}
});
}
}) I have just copy paste your functions (e.g. runQuery, runTransactQuery, rollbackOnError) as it is. |
@sapkal-manish what version of this library are you using and what version of Node.js? |
I have node-mysql (2.0.1) with nodejs (v0.10.37) |
ThreadId was not added until version 2.2.0. You will need to upgrade to get the threadId. |
Ok @dougwilson, that I have got it. but still I am not getting fetched row from mysql. In your "rollbackOnError" function, I have got rows with data (eg. console.log(arguments) shows me data), but when I tried in callback, there is only matadata comes |
Ah, I see the error in my example. I am currently on a cell phone and should be back to a computer next week. Please remind me then to update my example if I haven't responded by then. |
Or if you cannot figure out the fix to the example in the meantime :) |
* CHANGELOG: include v1.4.1 * Release v1.5.0
I have created pool cluster in my app as follows :
In short (about 1 question), I need on option in
getConnection()
method, to start TRANSACTION.The text was updated successfully, but these errors were encountered: