khoi
khoi

Reputation: 1180

Error: NJS-003: invalid connection node-oracledb

I want to insert data into the database. But I don't want to use autocommit:true because want to make sure another process able to completed first. But received an error message. I don't know what mistake I have made. Hope can help me solve this problem. Thank you in advance.

(node:2372) UnhandledPromiseRejectionWarning: Error: NJS-003: invalid connection at Connection.commit (C:\Users\user\Documents\GitHub\EIS-API\node_modules\oracledb\lib\connection.js:239:16) at C:\Users\user\Documents\GitHub\EIS-API\node_modules\oracledb\lib\util.js:202:16 at new Promise () at Connection.commit (C:\Users\user\Documents\GitHub\EIS-API\node_modules\oracledb\lib\util.js:190:14) at PostPemantauanPerkidmatanPotongRumputKonsesi (C:\Users\user\Documents\GitHub\EIS-API\routes\konsesi\konsesi.js:416:28) (node:2372) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:2372) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

 //want to check if file exists or not
               var filename1 = typeof files[0] === "undefined" ? "" : files[0].originalname; 
               var filename2 = typeof files[1] === "undefined" ? "" : files[1].originalname;
               var filename3 = typeof files[2] === "undefined" ? "" : files[2].originalname;
               var filename4 = typeof files[3] === "undefined" ? "" : files[3].originalname;


               let date = new Date();
               let connection2;
               try{
                    connection2 = await oracledb.getConnection(dbconfig);
                         // add information with image
                    const result = await connection2.execute(
                         `
                        INSERT INTO EIS.KON_KONRUMPUT
                        (
                             KON_ZONRUMPUT,KON_PAKEJKODS,KON_TAMANIDSS,KON_NAMATAMAN,
                             KON_JALANIDSS,KON_NAMAJALAN, KON_ADDRSYRKT,KON_SELIANAME,
                             KON_PTAUDATES,KON_BULANPTAU,KON_MASAPNTAU,KON_PUSINGANS,
                             KON_TIMESAMPM,KON_STATUSKOD,KON_CATATANSS,KON_TAHUNRPUT,
                             KON_ENTRYOPER,KON_ENTRYDATE,KON_STATUSFLG,
                             KON_GAMBAR111,KON_GAMBAR222,KON_GAMBAR333,KON_GAMBAR444
                         )
                         VALUES
                         ( :zon , :pakejKod , :tamanId , :tamanName , 
                           :jalanId , :jalanName , :alamat_syarikat ,:nama_penyelia , 
                           :tarikhPantau , :bulan , :masa , :pusingan , 
                           :timeAMPM , :status , :catatan , :tahun , 
                           :entryOperator , :tarikhPantau , :platform,
                           :gambar111 , :gambar222 , :gambar333 , :gambar444
                         )
                         `,{
                              zon:{val:params.zon} , pakejKod:{val:params.pakejKod} , tamanId:{val:params.tamanId} , tamanName:{val:params.tamanName},
                              jalanId:{val:params.jalanId} , jalanName:{val:params.jalanName} , alamat_syarikat:{val:_const.optionalParams(params.alamat_syarikat)} , nama_penyelia:{val:params.nama_penyelia},
                              tarikhPantau:{val:params.tarikhPantau} , bulan:{val:params.bulan}, masa:{val:params.masa} , pusingan:{val:params.pusingan},
                              timeAMPM:{val:params.timeAMPM} , status:{val:params.status} , catatan:{val:_const.optionalParams(params.catatan)} , tahun:{val:params.tahun},
                              entryOperator:{val:params.entryOperator} , tarikhPantau:{val:date},platform:{val:"A"},
                              gambar111:{val:filename1} , gambar222:{val:filename2} , gambar333:{val:filename3} , gambar444:{val:filename4}

                         }
                         );

                         Status.status = _const.MSG_STATUS_SUCCESS;
                         Status.message = "Data berjaya simpan..";
                         Status.info = result.rowsAffected;               

                    // send image to another server
                    files.forEach(image => {

                         var formData = {
                              image: fs.createReadStream("picture/" + image.originalname),
                            };

                             var data = request.post({url:'http://localhost/upload_file/', formData: formData}, async function(err, httpResponse, body) {
                              if(httpResponse.statusCode != 200){

                                   Status.message = err;

                                   //delete image
                                   var deleteImage = !isDeleteImage(files);

                                   //failed delete image
                                   if(!deleteImage.isSuccess){
                                        Status.message = deleteImage.message; 
                                   }
                                   return Status;               
                               }else{

                               }                             
                         });

                    });


               }catch(err){
                    console.error(err);
                    Status.message = err;
               }finally{
                    try{
                         await connection2.close();
                    }catch(err){
                         console.error(err);
                         Status.message = err;
                    }
               }
              //commit
               connection2.commit();

Upvotes: 0

Views: 7307

Answers (1)

Christopher Jones
Christopher Jones

Reputation: 10496

From quick inspection, your finally block is closing the connection before the commit is reached.

Upvotes: 1

Related Questions