mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
369 stars 91 forks source link

Connection stops responding after exception thrown by 'batch' operation. #259

Closed markddrake closed 5 months ago

markddrake commented 11 months ago

Running the Following Code

"use strict"
import mariadb from 'mariadb';
import fs from 'fs';

async function main() {

    const connectionDetails = {
            host      : "yadamu-db2"
           ,user      : "root"
           ,password  : "oracle"
           ,port      : 3307
           ,database  : "mysql"
           ,multipleStatements: true
           ,typeCast:true
           ,bigNumberStrings:true
           ,dateStrings:true
           ,rowsAsArray:true}

  let results;

  try {

    const pool = mariadb.createPool(connectionDetails);
    const conn = await pool.getConnection();
    results = await conn.query(`SET AUTOCOMMIT = 0, TIME_ZONE = '+00:00',SESSION INTERACTIVE_TIMEOUT = 600000, WAIT_TIMEOUT = 600000, SQL_MODE='ANSI_QUOTES,PAD_CHAR_TO_FULL_LENGTH', GROUP_CONCAT_MAX_LEN = 1024000, GLOBAL LOCAL_INFILE = 'ON';`);
    results = await conn.query(`drop schema if exists "PM";`);    
    results = await conn.query(`create schema "PM";`);    

    results = await conn.query(`USE "PM"`);    
    results = await conn.query(`create table  if not exists "PM"."ONLINE_MEDIA"("PRODUCT_ID" decimal(6) ,"PRODUCT_PHOTO" longtext ,"PRODUCT_PHOTO_SIGNATURE" longtext ,"PRODUCT_THUMBNAIL" longtext ,"PRODUCT_VIDEO" longtext ,"PRODUCT_AUDIO" longtext ,"PRODUCT_TEXT" longtext ,"PRODUCT_TESTIMONIALS" longtext )`);

    const testData = fs.readFileSync('ONLINE_MEDIA.json');
    const data = JSON.parse(testData)
    console.log('ROWS',data.length)

    const sql = `insert into "PM"."ONLINE_MEDIA" ("PRODUCT_ID","PRODUCT_PHOTO","PRODUCT_PHOTO_SIGNATURE","PRODUCT_THUMBNAIL","PRODUCT_VIDEO","PRODUCT_AUDIO","PRODUCT_TEXT","PRODUCT_TESTIMONIALS") values  (?,?,?,?,?,?,?,?)`

    try {

      results = await conn.beginTransaction()
      results = await conn.query(`SAVEPOINT YADAMU_INSERT`)
      results = await conn.batch(sql,data);
      results = await conn.commit()
    } catch (e) {
      console.log(e)
      console.log('Atttempting Rollback to SAVEPOINT')
      try {
        results = await conn.query(`ROLLBACK TO SAVEPOINT YADAMU_INSERT`)
        console.log('Wish I could get here')  
      }
      catch (e) {
        console.log('Even here  would be useful :)')  
        console.log(e)
      }
    } 

   conn.end()
   pool.end();
 } catch(e) {
   console.log(e);
 }
}

main().then(() => {console.log("Success")}).catch((e) => {console.log(e)})

Results in the following

C:\Development\YADAMU\src\scratch\mariadb>node batchIssue2.js
ROWS 9
TypeError: Cannot read properties of undefined (reading '0')
    at BatchBulk.sendComStmtBulkExecute (C:\Development\YADAMU\src\node_modules\mariadb\lib\cmd\batch-bulk.js:261:32)
    at BatchBulk.start (C:\Development\YADAMU\src\node_modules\mariadb\lib\cmd\batch-bulk.js:61:10)
    at Connection.addCommandEnablePipeline (C:\Development\YADAMU\src\node_modules\mariadb\lib\connection.js:1087:11)
    at Connection.executeBulkPromise (C:\Development\YADAMU\src\node_modules\mariadb\lib\connection.js:286:10)
    at new Promise (<anonymous>)
    at C:\Development\YADAMU\src\node_modules\mariadb\lib\connection.js:210:16
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async main (file:///C:/Development/YADAMU/src/scratch/mariadb/batchIssue2.js:43:17)
Atttempting Rollback to SAVEPOINT
^C
C:\Development\YADAMU\src\scratch\mariadb>

The line

    results = await conn.query(`ROLLBACK TO SAVEPOINT YADAMU_INSERT`)

never returns contol to the calling program. It is necessary to Cntrl-C the program in order to regain control. The testdata needed to run this example is connected to  https://github.com/mariadb-corporation/mariadb-connector-nodejs/issues/258

rusher commented 5 months ago

closing since correction is released in 3.3.0