oracle / node-oracledb

Oracle Database driver for Node.js maintained by Oracle Corp.
http://oracle.github.io/node-oracledb/
Other
2.25k stars 1.07k forks source link

NJS-003:invalid connection node:internal/errors:464 ErrorCaptureStackTrace(err) #1458

Closed fikriharjo closed 2 years ago

fikriharjo commented 2 years ago

Hello guys, can you help me to solve my problem? im using node v17.0.1 and sometimes my db crash and show error message like this NJS-003:invalid connection node:internal/errors:464 ErrorCaptureStackTrace(err).

im using loop to insert data from my application with this code:

async function insert_wo(req, res, data) { try { connection = await oracledb.getConnection(db_configuration); data[1] = "'"+" "+" "+" "+" "+" "+" "+" "+" "+" "+data[1]+"'";

    var production_start_date = new Date(data[5])
    var bulan = production_start_date.getMonth()+1;
    if (bulan == 1) {
        bulan = 'JAN';
    } else if(bulan == 2){
        bulan = 'FEB';
    } else if(bulan == 3){
        bulan = 'MAR';
    } else if(bulan == 4){
        bulan = 'APR';
    } else if(bulan == 5){
        bulan = 'MAY';
    } else if(bulan == 6){
        bulan = 'JUN';
    } else if(bulan == 7){
        bulan = 'JUL';
    } else if(bulan == 8){
        bulan = 'AUG';
    } else if(bulan == 9){
        bulan = 'SEP';
    } else if(bulan == 10){
        bulan = 'OCT';
    } else if(bulan == 11){
        bulan = 'NOV';
    } else {
        bulan = 'DEC';
    }

    var tanggal = production_start_date.getDate();
    tanggal = String(tanggal);
    if(tanggal.length == 1){
        tanggal = '0'+tanggal;
    }
    production_start_date = tanggal+'-'+bulan+'-'+production_start_date.getFullYear();

    var delivery_plan = new Date(data[5])
    var jam_awal = '';
    var jam_akhir = '';
    if (Number(data[9]) == 3) {
        delivery_plan.setDate(delivery_plan.getDate()+1);
        jam_awal = '16:59';
        jam_akhir = '00:30';
    } else if(Number(data[9]) == 2) {
        jam_awal = '09:30';
        jam_akhir = '16:00';
    } else {
        jam_awal = '00:30';
        jam_akhir = '09:30';
    }

    tanggal = delivery_plan.getDate();
    tanggal = String(tanggal);
    if(tanggal.length == 1){
        tanggal = '0'+tanggal;
    }

    bulan = delivery_plan.getMonth()+1;
    if (bulan == 1) {
        bulan = 'JAN';
    } else if(bulan == 2){
        bulan = 'FEB';
    } else if(bulan == 3){
        bulan = 'MAR';
    } else if(bulan == 4){
        bulan = 'APR';
    } else if(bulan == 5){
        bulan = 'MAY';
    } else if(bulan == 6){
        bulan = 'JUN';
    } else if(bulan == 7){
        bulan = 'JUL';
    } else if(bulan == 8){
        bulan = 'AUG';
    } else if(bulan == 9){
        bulan = 'SEP';
    } else if(bulan == 10){
        bulan = 'OCT';
    } else if(bulan == 11){
        bulan = 'NOV';
    } else {
        bulan = 'DEC';
    }

    production_start_date = production_start_date+' '+jam_awal;
    delivery_plan = tanggal+'-'+bulan+'-'+delivery_plan.getFullYear()+' '+jam_akhir;
    // delivery_plan = tanggal+'-'+bulan+'-'+delivery_plan.getFullYear();

    data[6] = data[6].substring(0, 9);
    data[7] = data[7].substring(0, 3);
    data[7] = data[7].toUpperCase();
    // console.log(production_start_date)
    result = await connection.execute("Insert into a (a.t$apdt, a.t$prwo, a.t$mitm, a.t$opro, a.t$cwar, a.t$qrdr, a.t$prdt, a.t$dldt, a.t$nrfq, a.t$plid, a.t$pdno, a.t$ketr, a.t$stat, a.t$grp1, a.t$grp2, a.t$grp3, a.t$line, a.t$refcntd, a.t$refcntu) values (TO_DATE(TO_CHAR(SYSDATE - (7/24), 'DD-MON-YYYY HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS'), '"+data[0]+"', "+data[1]+", "+data[2]+", '"+data[3]+"', "+data[4]+", TO_DATE(\'"+production_start_date+"\', \'dd/mm/yyyy hh24:mi:ss\'), TO_DATE(\'"+delivery_plan+"\', \'dd/mm/yyyy hh24:mi:ss\'), '"+data[6]+"', '"+data[7]+"', ' ', ' ', 2, '"+data[8]+"', '"+data[10]+"', ' ',"+data[11]+", 1, 1)", [], { autoCommit: true});
    console.log("Rows inserted: " + delivery_plan);
} catch (err) {
    //send error message
    return res.send(err.message);
} finally {
    if (connection) {
        try {
            // Always close connections
            await connection.close();
            return res.send('success');
        } catch (err) {
            return res.send(err);
        }
    }
}

}

cjbj commented 2 years ago

@fikriharjo start by simplifying your test case. Remove any line that doesn't impact the behavior. Make sure to add a let connection in the correct spot before the try, so you are closing the correct connection.

Then change your INSERT to use bind variables, see all the examples in the doc.

fikriharjo commented 2 years ago

Its done. Just because i run terminal in vs code before and now i run it in terminal and everythings fine