Javascript 使用节点mysql进行同步查询
我有一个Javascript程序正在查询MySQL表,我不明白为什么同时查询的结果没有得到很好的处理 一个对象、分区有多个实例。在实例化对象时,我查询数据库以获取一些信息 我的问题是,当我同时实例化两个对象时,第一个对象的查询结果会写入第二个对象 我尝试使用一个池创建两个不同的连接,但它没有改变任何事情 这是我的密码: 进入数据库:Javascript 使用节点mysql进行同步查询,javascript,mysql,node.js,connection-pooling,node-mysql,Javascript,Mysql,Node.js,Connection Pooling,Node Mysql,我有一个Javascript程序正在查询MySQL表,我不明白为什么同时查询的结果没有得到很好的处理 一个对象、分区有多个实例。在实例化对象时,我查询数据库以获取一些信息 我的问题是,当我同时实例化两个对象时,第一个对象的查询结果会写入第二个对象 我尝试使用一个池创建两个不同的连接,但它没有改变任何事情 这是我的密码: 进入数据库: getOrdresAchat = function(aConnection, aIdZone, aCallback){ aConnection.query(
getOrdresAchat = function(aConnection, aIdZone, aCallback){
aConnection.query("SELECT * FROM Ordre WHERE Entreprise_idEntreprise = "+aIdZone+" AND Sens = 'Achat' ORDER BY Valeur DESC;", aCallback);
}
//No need to check that object
Ordre = function(aNumero, aJoueur, aEntreprise, aSens, aTypeOrdre, aQuantite, aPrix, aBorneInf, aBorneSup) {
this.numero = aNumero;
this.entreprise = aEntreprise;
this.joueur = aJoueur;
this.sens = aSens;
this.typeOrdre = aTypeOrdre; // 'cours_limite', 'meilleure_limite', 'seuil_declenchement' ou 'au_marche'
this.quantite = aQuantite; // nombre entier positif
this.prix = aPrix; //si le prix est nul il est considéré comme sans limite
this.borneInf = aBorneInf;
this.borneSup = aBorneSup;
}
//kinda an array of Ordre
Carnet_achat = function() {
this.liste = [];
this.addElement = function(aOrdre) {
this.liste.push(aOrdre);
};
this.display= function(){
console.log("begining carnet_achat.display | List.length : "+this.liste.length);
for(i in this.liste){
console.log("row "+i+" : " +this.liste[i].prix);
}
};
}
Zone = function(aIdZone, aConnection) {
var idZone = aIdZone;
this.setIdZone = function(aNewId){
idZone = aNewId;
}
this.getIdZone =function(){
return idZone;
}
this.getCarnetAchat = function(aConnection){
rCarnet_achat = new Carnet_achat(); //rCarnet is temporary object
//we query database and push every row into the temp object
getOrdresAchat(aConnection, idZone, function(err, rows){
if(err) throw err;
for(i in rows){
ordreToAdd = new Ordre(rows[i].idOrdre,
rows[i].Joueur_idJoueur,
rows[i].Entreprise_idEntreprise,
rows[i].Sens,
rows[i].Type,
rows[i].Quantite,
rows[i].Valeur,
rows[i].BorneInf,
rows[i].BorneSup);
rCarnet_achat.addElement(ordreToAdd);
}
});
return rCarnet_achat;
}
//init
this.carnet_achat = new this.getCarnetAchat(aConnection);
}
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'localhost',
user : 'Bibacoeur',
password : 'Bibacoeur2014',
database : 'bibacoeur'
});
pool.getConnection(function(err, connectionA) {
if(err) throw err;
GI = new Zone(1, connectionA); //connection is used to query database
//timeOut to wait the end of the query
setTimeout(function(){console.log("GI_ACHAT");GI.carnet_achat.display();},3000);
});
pool.getConnection(function(err, connectionB) {
if(err) throw err;
GE = new Zone(2, connectionB); //connection is used to query database
//timeOut to wait the end of the query
setTimeout(function(){console.log("GE_ACHAT");GE.carnet_achat.display();},3000);
});
C:\Users\QuentinB\Google Drive\Desktop\temp>node app.js
GI_ACHAT
begining carnet_achat.display | List.length : 0
GE_ACHAT
begining carnet_achat.display | List.length : 4
row 0 : 90500
row 1 : 90500
row 2 : 88500
row 3 : 0
对象定义:
getOrdresAchat = function(aConnection, aIdZone, aCallback){
aConnection.query("SELECT * FROM Ordre WHERE Entreprise_idEntreprise = "+aIdZone+" AND Sens = 'Achat' ORDER BY Valeur DESC;", aCallback);
}
//No need to check that object
Ordre = function(aNumero, aJoueur, aEntreprise, aSens, aTypeOrdre, aQuantite, aPrix, aBorneInf, aBorneSup) {
this.numero = aNumero;
this.entreprise = aEntreprise;
this.joueur = aJoueur;
this.sens = aSens;
this.typeOrdre = aTypeOrdre; // 'cours_limite', 'meilleure_limite', 'seuil_declenchement' ou 'au_marche'
this.quantite = aQuantite; // nombre entier positif
this.prix = aPrix; //si le prix est nul il est considéré comme sans limite
this.borneInf = aBorneInf;
this.borneSup = aBorneSup;
}
//kinda an array of Ordre
Carnet_achat = function() {
this.liste = [];
this.addElement = function(aOrdre) {
this.liste.push(aOrdre);
};
this.display= function(){
console.log("begining carnet_achat.display | List.length : "+this.liste.length);
for(i in this.liste){
console.log("row "+i+" : " +this.liste[i].prix);
}
};
}
Zone = function(aIdZone, aConnection) {
var idZone = aIdZone;
this.setIdZone = function(aNewId){
idZone = aNewId;
}
this.getIdZone =function(){
return idZone;
}
this.getCarnetAchat = function(aConnection){
rCarnet_achat = new Carnet_achat(); //rCarnet is temporary object
//we query database and push every row into the temp object
getOrdresAchat(aConnection, idZone, function(err, rows){
if(err) throw err;
for(i in rows){
ordreToAdd = new Ordre(rows[i].idOrdre,
rows[i].Joueur_idJoueur,
rows[i].Entreprise_idEntreprise,
rows[i].Sens,
rows[i].Type,
rows[i].Quantite,
rows[i].Valeur,
rows[i].BorneInf,
rows[i].BorneSup);
rCarnet_achat.addElement(ordreToAdd);
}
});
return rCarnet_achat;
}
//init
this.carnet_achat = new this.getCarnetAchat(aConnection);
}
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'localhost',
user : 'Bibacoeur',
password : 'Bibacoeur2014',
database : 'bibacoeur'
});
pool.getConnection(function(err, connectionA) {
if(err) throw err;
GI = new Zone(1, connectionA); //connection is used to query database
//timeOut to wait the end of the query
setTimeout(function(){console.log("GI_ACHAT");GI.carnet_achat.display();},3000);
});
pool.getConnection(function(err, connectionB) {
if(err) throw err;
GE = new Zone(2, connectionB); //connection is used to query database
//timeOut to wait the end of the query
setTimeout(function(){console.log("GE_ACHAT");GE.carnet_achat.display();},3000);
});
C:\Users\QuentinB\Google Drive\Desktop\temp>node app.js
GI_ACHAT
begining carnet_achat.display | List.length : 0
GE_ACHAT
begining carnet_achat.display | List.length : 4
row 0 : 90500
row 1 : 90500
row 2 : 88500
row 3 : 0
main:
getOrdresAchat = function(aConnection, aIdZone, aCallback){
aConnection.query("SELECT * FROM Ordre WHERE Entreprise_idEntreprise = "+aIdZone+" AND Sens = 'Achat' ORDER BY Valeur DESC;", aCallback);
}
//No need to check that object
Ordre = function(aNumero, aJoueur, aEntreprise, aSens, aTypeOrdre, aQuantite, aPrix, aBorneInf, aBorneSup) {
this.numero = aNumero;
this.entreprise = aEntreprise;
this.joueur = aJoueur;
this.sens = aSens;
this.typeOrdre = aTypeOrdre; // 'cours_limite', 'meilleure_limite', 'seuil_declenchement' ou 'au_marche'
this.quantite = aQuantite; // nombre entier positif
this.prix = aPrix; //si le prix est nul il est considéré comme sans limite
this.borneInf = aBorneInf;
this.borneSup = aBorneSup;
}
//kinda an array of Ordre
Carnet_achat = function() {
this.liste = [];
this.addElement = function(aOrdre) {
this.liste.push(aOrdre);
};
this.display= function(){
console.log("begining carnet_achat.display | List.length : "+this.liste.length);
for(i in this.liste){
console.log("row "+i+" : " +this.liste[i].prix);
}
};
}
Zone = function(aIdZone, aConnection) {
var idZone = aIdZone;
this.setIdZone = function(aNewId){
idZone = aNewId;
}
this.getIdZone =function(){
return idZone;
}
this.getCarnetAchat = function(aConnection){
rCarnet_achat = new Carnet_achat(); //rCarnet is temporary object
//we query database and push every row into the temp object
getOrdresAchat(aConnection, idZone, function(err, rows){
if(err) throw err;
for(i in rows){
ordreToAdd = new Ordre(rows[i].idOrdre,
rows[i].Joueur_idJoueur,
rows[i].Entreprise_idEntreprise,
rows[i].Sens,
rows[i].Type,
rows[i].Quantite,
rows[i].Valeur,
rows[i].BorneInf,
rows[i].BorneSup);
rCarnet_achat.addElement(ordreToAdd);
}
});
return rCarnet_achat;
}
//init
this.carnet_achat = new this.getCarnetAchat(aConnection);
}
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'localhost',
user : 'Bibacoeur',
password : 'Bibacoeur2014',
database : 'bibacoeur'
});
pool.getConnection(function(err, connectionA) {
if(err) throw err;
GI = new Zone(1, connectionA); //connection is used to query database
//timeOut to wait the end of the query
setTimeout(function(){console.log("GI_ACHAT");GI.carnet_achat.display();},3000);
});
pool.getConnection(function(err, connectionB) {
if(err) throw err;
GE = new Zone(2, connectionB); //connection is used to query database
//timeOut to wait the end of the query
setTimeout(function(){console.log("GE_ACHAT");GE.carnet_achat.display();},3000);
});
C:\Users\QuentinB\Google Drive\Desktop\temp>node app.js
GI_ACHAT
begining carnet_achat.display | List.length : 0
GE_ACHAT
begining carnet_achat.display | List.length : 4
row 0 : 90500
row 1 : 90500
row 2 : 88500
row 3 : 0
数据库:
mysql> select Entreprise_idEntreprise, Sens, Valeur from ordre;
+-------------------------+-------+--------+
| Entreprise_idEntreprise | Sens | Valeur |
+-------------------------+-------+--------+
| 1 | Vente | 0 |
| 1 | Vente | 90000 |
| 1 | Vente | 91000 |
| 1 | Vente | 92000 |
| 1 | Vente | 95000 |
| 1 | Vente | 100000 |
| 1 | Achat | 88500 |
| 1 | Achat | 90500 |
| 1 | Achat | 90500 |
| 2 | Achat | 0 |
+-------------------------+-------+--------+
10 rows in set (0.00 sec)
结果日志:
getOrdresAchat = function(aConnection, aIdZone, aCallback){
aConnection.query("SELECT * FROM Ordre WHERE Entreprise_idEntreprise = "+aIdZone+" AND Sens = 'Achat' ORDER BY Valeur DESC;", aCallback);
}
//No need to check that object
Ordre = function(aNumero, aJoueur, aEntreprise, aSens, aTypeOrdre, aQuantite, aPrix, aBorneInf, aBorneSup) {
this.numero = aNumero;
this.entreprise = aEntreprise;
this.joueur = aJoueur;
this.sens = aSens;
this.typeOrdre = aTypeOrdre; // 'cours_limite', 'meilleure_limite', 'seuil_declenchement' ou 'au_marche'
this.quantite = aQuantite; // nombre entier positif
this.prix = aPrix; //si le prix est nul il est considéré comme sans limite
this.borneInf = aBorneInf;
this.borneSup = aBorneSup;
}
//kinda an array of Ordre
Carnet_achat = function() {
this.liste = [];
this.addElement = function(aOrdre) {
this.liste.push(aOrdre);
};
this.display= function(){
console.log("begining carnet_achat.display | List.length : "+this.liste.length);
for(i in this.liste){
console.log("row "+i+" : " +this.liste[i].prix);
}
};
}
Zone = function(aIdZone, aConnection) {
var idZone = aIdZone;
this.setIdZone = function(aNewId){
idZone = aNewId;
}
this.getIdZone =function(){
return idZone;
}
this.getCarnetAchat = function(aConnection){
rCarnet_achat = new Carnet_achat(); //rCarnet is temporary object
//we query database and push every row into the temp object
getOrdresAchat(aConnection, idZone, function(err, rows){
if(err) throw err;
for(i in rows){
ordreToAdd = new Ordre(rows[i].idOrdre,
rows[i].Joueur_idJoueur,
rows[i].Entreprise_idEntreprise,
rows[i].Sens,
rows[i].Type,
rows[i].Quantite,
rows[i].Valeur,
rows[i].BorneInf,
rows[i].BorneSup);
rCarnet_achat.addElement(ordreToAdd);
}
});
return rCarnet_achat;
}
//init
this.carnet_achat = new this.getCarnetAchat(aConnection);
}
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'localhost',
user : 'Bibacoeur',
password : 'Bibacoeur2014',
database : 'bibacoeur'
});
pool.getConnection(function(err, connectionA) {
if(err) throw err;
GI = new Zone(1, connectionA); //connection is used to query database
//timeOut to wait the end of the query
setTimeout(function(){console.log("GI_ACHAT");GI.carnet_achat.display();},3000);
});
pool.getConnection(function(err, connectionB) {
if(err) throw err;
GE = new Zone(2, connectionB); //connection is used to query database
//timeOut to wait the end of the query
setTimeout(function(){console.log("GE_ACHAT");GE.carnet_achat.display();},3000);
});
C:\Users\QuentinB\Google Drive\Desktop\temp>node app.js
GI_ACHAT
begining carnet_achat.display | List.length : 0
GE_ACHAT
begining carnet_achat.display | List.length : 4
row 0 : 90500
row 1 : 90500
row 2 : 88500
row 3 : 0
应该是:
C:\Users\QuentinB\Google Drive\Desktop\temp>node app.js
GI_ACHAT
begining carnet_achat.display | List.length : 3
row 0 : 90500
row 1 : 90500
row 2 : 88500
GE_ACHAT
begining carnet_achat.display | List.length : 1
row 0 : 0
从第一个对象GI执行的查询结果似乎被第二个对象GE接收
我试图用以下命令延迟第二个对象的创建:setTimeout(function(){GE=newzone(2,connectionB);},10)代码>成功了。
但我不想延迟每次创建,因为我还有40多个对象要实例化
如果我想确保查询结果被正确的对象接收,我该怎么办
谢谢你的阅读 由于您没有使用var
关键字声明rCarnet\u achat
,因此您隐式地将其声明为全局变量。因此,每次调用rCarnet\u achat.addElement(ordreToAdd)代码>,实际上是在向同一个Carnet\u achat
实例添加元素。这可以通过添加var rCarnet\u achat代码>到区域的顶部
功能
p、 您的代码还存在其他问题,例如
- 您应该使用的查询字符串转义来避免SQL注入攻击,例如,
aConnection.query(“从Ordre中选择*,其中enterprise_identirese=?和Sens='Achat'ORDER BY Valeur DESC;”,aIdZone,aCallback)
- 使用setTimeout等待SQL查询完成是错误的。就等着回电话吧
<> LI>由于“有40个对象要进行实例化”,您可能会考虑编写一个查询来同时获取它们,例如从FO中选择“代码”>“选择”*,其中ID在(1,2,3)中;代码>
“我有一个Java程序”-不,你没有。对不起!编辑你是我的救世主!非常感谢