[英]How to get info about connection Id at request?
I want to get the connection Id (threadId) when executing long query just in case the user at frontend (Vue.js) wants to cancel the query (in case query never ends...). 我想在执行长查询时获取连接ID(threadId),以防前端(Vue.js)的用户想要取消查询(以防查询永无止境...)。 So when the user clicks "Stop query", I would perform KILL (threadId) on Mysql.. 因此,当用户单击“停止查询”时,我将在Mysql上执行KILL(threadId)。
I am using pooling with knew (client=mysql). 我正在使用已知的池(client = mysql)。 I believe this is something trivial, but can please someone describe how can I access this. 我相信这是一件微不足道的事情,但是请有人描述我该如何使用它。 my backend (node.js) is set up differently - -- was made by a clever guy, who defined pools and connection as follows: 我的后端(node.js)的设置有所不同-是由一个聪明的家伙制作的,他定义了池和连接,如下所示:
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
const knex = require("knex");
// Helpers -------------------------------------------------------------------
const connection = (user, password) =>
knex({
client: "mysql",
connection: {
multipleStatements: true,
host: process.env.MYSQL_IP,
port: process.env.MYSQL_PORT,
user,
password,
database: "",
dateStrings: true,
idleTimeoutMillis: -1
},
debug: true,
pool: {
min: 0,
max: 7
}
});
const knex_session = pool => (user, password) => {
if (pool[user] === undefined) {
pool[user] = connection(user, password);
}
return pool[user];
};
// Pool ----------------------------------------------------------------------
const connections = {};
// Exports -------------------------------------------------------------------
exports.admin = connection(
process.env.MYSQL_USERNAME,
process.env.MYSQL_PASSWORD
);
exports.client = knex_session(connections);
// ---------------------------------------------------------------------------
I am looking for a way to get the mysql threadId somehow - I imagine from the function(route). 我正在寻找一种以某种方式获取mysql threadId的方法-我从function(route)中想象到了。 Orther options are aother call to mysql (SELECT CONNECTION_ID()) or by somehow extracting his data from request object (however, was not successful here as well). 其他选项是对mysql的另一次调用(SELECT CONNECTION_ID())或通过某种方式从请求对象中提取了他的数据(但是,在这里也不成功)。 Than I would execute KILL QUERY (threadId) to make sure that the query does not hang and that I can display proper vue component to the user. 比我要执行KILL QUERY(threadId)来确保查询不会挂起,并且可以向用户显示适当的vue组件。
As you have discovered, the MySQL connection id is found as an attribute of the connection object: connection.threadId
. 您已经发现, MySQL连接ID被视为连接对象的属性: connection.threadId
。 That's true for knex , because it's layered on mysql . 对于knex来说确实如此,因为它位于mysql之上。
You can grab it from the connection object whenever it's in scope and put it in a useful place ... something like this if you're in express. 只要它在作用域内,就可以从连接对象中获取它,并将其放在有用的地方……如果您是表达的话,则类似这样。
function whatever (req, res, next) {
...
res.dbmsThreadId = connection.threadId
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.