node-firebird
node-firebird copied to clipboard
Problem with doing various transactions
I'm using the transaction to send inserts to the database and if only all of them are correct, they confirm, if not, it gives roolback on everything
I made my code as the documentation asks, but I need to commit after all they are right, not one by one, am I doing wrong? Is there another way to make multiple inserts and confirm it only when all are correct? I'm sending my code to parse, I've tried it another way, but they all give me some kind of error. This way I'm submitting, it inserts in the first transaction, but not in the second. ` firebird.attach(configuracoes, function (err, db) {
db.transaction(firebird.ISOLATION_READ_COMMITED, function (err, transaction) {
for (var cont = 0; cont < anexo.length; cont++) {
transaction.query('INSERT INTO TPESSOAANEXO (PAN_CODIGO,PAN_TITULO, PES_CODIGO,PAN_DOCUMENTO)'
+ 'VALUES (gen_id(GEN_PESSOAANEXO,1),?,?,?)', [anexo[cont].ANE_DESCRICAO, anexo[cont].COD_TITULAR, anexo[cont].ANE_IMAGEM], function (err, result) {
for(var cont = 0; cont< anexo.length; cont ++){
transaction.query('INSERT INTO TESTE (PAN_CODIGO,PAN_TITULO, PES_CODIGO,PAN_DOCUMENTO)'
+ 'VALUES (gen_id(GEN_PESSOAANEXO,1),?,?,?)',[anexo[cont].TESTE_DESCRICAO, anexo[cont].TESTE_TITULAR,anexo[cont].TESTE_IMAGEM], function (err, result) {
})
}
transaction.commit(function (err) {
if (err) {
transaction.rollback();
} else
res.status(200).json('Dados incluidos com sucesso')
db.detach();
});
});
}
});
}); `
You need to serialise the transaction.query calls. Put them in a async.eachSeries call with the anexo. like: async.eachSeries(anexo, function(aobj, cb) { ...
On Tue, Sep 10, 2019 at 4:58 AM Eloysa Consoni [email protected] wrote:
I'm using the transaction to send inserts to the database and if only all of them are correct, they confirm, if not, it gives roolback on everything
I made my code as the documentation asks, but I need to commit after all they are right, not one by one, am I doing wrong? Is there another way to make multiple inserts and confirm it only when all are correct? I'm sending my code to parse, I've tried it another way, but they all give me some kind of error. This way I'm submitting, it inserts in the first transaction, but not in the second. ` firebird.attach(configuracoes, function (err, db) {
db.transaction(firebird.ISOLATION_READ_COMMITED, function (err, transaction) { for (var cont = 0; cont < anexo.length; cont++) { transaction.query('INSERT INTO TPESSOAANEXO (PAN_CODIGO,PAN_TITULO, PES_CODIGO,PAN_DOCUMENTO)' + 'VALUES (gen_id(GEN_PESSOAANEXO,1),?,?,?)', [anexo[cont].ANE_DESCRICAO, anexo[cont].COD_TITULAR, anexo[cont].ANE_IMAGEM], function (err, result) {
for(var cont = 0; cont< anexo.length; cont ++){ transaction.query('INSERT INTO TESTE (PAN_CODIGO,PAN_TITULO, PES_CODIGO,PAN_DOCUMENTO)' + 'VALUES (gen_id(GEN_PESSOAANEXO,1),?,?,?)',[anexo[cont].TESTE_DESCRICAO, anexo[cont].TESTE_TITULAR,anexo[cont].TESTE_IMAGEM], function (err, result) { }) } transaction.commit(function (err) { if (err) { transaction.rollback(); } else res.status(200).json('Dados incluidos com sucesso') db.detach(); }); }); }
});
}); `
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/hgourvest/node-firebird/issues/173?email_source=notifications&email_token=ABSJIDM3IQ5UHK22LUDZQF3QI2MGTA5CNFSM4IU65CTKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HKIFHBA, or mute the thread https://github.com/notifications/unsubscribe-auth/ABSJIDN5VDILXWRHMTM73ZLQI2MGTANCNFSM4IU65CTA .
Você precisa serializar as chamadas transaction.query. Coloque-os em uma chamada async.eachSeries com o anexo. como: async.eachSeries (anexo, function (aobj, cb) {... ... On Tue, Sep 10, 2019 at 4:58 AM Eloysa Consoni @.***> wrote: I'm using the transaction to send inserts to the database and if only all of them are correct, they confirm, if not, it gives roolback on everything I made my code as the documentation asks, but I need to commit after all they are right, not one by one, am I doing wrong? Is there another way to make multiple inserts and confirm it only when all are correct? I'm sending my code to parse, I've tried it another way, but they all give me some kind of error. This way I'm submitting, it inserts in the first transaction, but not in the second.
firebird.attach(configuracoes, function (err, db) { db.transaction(firebird.ISOLATION_READ_COMMITED, function (err, transaction) { for (var cont = 0; cont < anexo.length; cont++) { transaction.query('INSERT INTO TPESSOAANEXO (PAN_CODIGO,PAN_TITULO, PES_CODIGO,PAN_DOCUMENTO)' + 'VALUES (gen_id(GEN_PESSOAANEXO,1),?,?,?)', [anexo[cont].ANE_DESCRICAO, anexo[cont].COD_TITULAR, anexo[cont].ANE_IMAGEM], function (err, result) { for(var cont = 0; cont< anexo.length; cont ++){ transaction.query('INSERT INTO TESTE (PAN_CODIGO,PAN_TITULO, PES_CODIGO,PAN_DOCUMENTO)' + 'VALUES (gen_id(GEN_PESSOAANEXO,1),?,?,?)',[anexo[cont].TESTE_DESCRICAO, anexo[cont].TESTE_TITULAR,anexo[cont].TESTE_IMAGEM], function (err, result) { }) } transaction.commit(function (err) { if (err) { transaction.rollback(); } else res.status(200).json('Dados incluidos com sucesso') db.detach(); }); }); } }); });
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#173?email_source=notifications&email_token=ABSJIDM3IQ5UHK22LUDZQF3QI2MGTA5CNFSM4IU65CTKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HKIFHBA>, or mute the thread https://github.com/notifications/unsubscribe-auth/ABSJIDN5VDILXWRHMTM73ZLQI2MGTANCNFSM4IU65CTA .
Hi thanks for responding, I read about async and tested it but I believe it is not what I need, I just need to read my transactions and only if all are correct that he commits and if any is wrong he gives the rollback at all, which I can't do since he only reads and answers one query at a time.
You cannot use a for-loop here. You will get intermittent Firebird deadlock errors. You need to serialise the FB calls. Just warning you. The async.EachSeries has a callback that gets called at the end. This will do the trick for you.
On Wed, Sep 11, 2019 at 4:16 AM Eloysa Consoni [email protected] wrote:
Você precisa serializar as chamadas transaction.query. Coloque-os em uma chamada async.eachSeries com o anexo. como: async.eachSeries (anexo, function (aobj, cb) {... ... <#m_693246162990103516_> On Tue, Sep 10, 2019 at 4:58 AM Eloysa Consoni @.***> wrote: I'm using the transaction to send inserts to the database and if only all of them are correct, they confirm, if not, it gives roolback on everything I made my code as the documentation asks, but I need to commit after all they are right, not one by one, am I doing wrong? Is there another way to make multiple inserts and confirm it only when all are correct? I'm sending my code to parse, I've tried it another way, but they all give me some kind of error. This way I'm submitting, it inserts in the first transaction, but not in the second. firebird.attach(configuracoes, function (err, db) { db.transaction(firebird.ISOLATION_READ_COMMITED, function (err, transaction) { for (var cont = 0; cont < anexo.length; cont++) { transaction.query('INSERT INTO TPESSOAANEXO (PAN_CODIGO,PAN_TITULO, PES_CODIGO,PAN_DOCUMENTO)' + 'VALUES (gen_id(GEN_PESSOAANEXO,1),?,?,?)', [anexo[cont].ANE_DESCRICAO, anexo[cont].COD_TITULAR, anexo[cont].ANE_IMAGEM], function (err, result) { for(var cont = 0; cont< anexo.length; cont ++){ transaction.query('INSERT INTO TESTE (PAN_CODIGO,PAN_TITULO, PES_CODIGO,PAN_DOCUMENTO)' + 'VALUES (gen_id(GEN_PESSOAANEXO,1),?,?,?)',[anexo[cont].TESTE_DESCRICAO, anexo[cont].TESTE_TITULAR,anexo[cont].TESTE_IMAGEM], function (err, result) { }) } transaction.commit(function (err) { if (err) { transaction.rollback(); } else res.status(200).json('Dados incluidos com sucesso') db.detach(); }); }); } }); }); — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#173 https://github.com/hgourvest/node-firebird/issues/173?email_source=notifications&email_token=ABSJIDM3IQ5UHK22LUDZQF3QI2MGTA5CNFSM4IU65CTKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HKIFHBA>, or mute the thread https://github.com/notifications/unsubscribe-auth/ABSJIDN5VDILXWRHMTM73ZLQI2MGTANCNFSM4IU65CTA .
Hi thanks for responding, I read about async and tested it but I believe it is not what I need, I just need to read my transactions and only if all are correct that he commits and if any is wrong he gives the rollback at all, which I can't do since he only reads and answers one query at a time.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hgourvest/node-firebird/issues/173?email_source=notifications&email_token=ABSJIDIEHCUUPMFLDU7ILE3QI7QBLA5CNFSM4IU65CTKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6MAZQY#issuecomment-530058435, or mute the thread https://github.com/notifications/unsubscribe-auth/ABSJIDMRV6VY27DQGZMF4FTQI7QBLANCNFSM4IU65CTA .