question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"read ECONNRESET" error is being thrown when trying to insert large size files.

See original GitHub issue

Hi, when I am trying to insert large size files I’m getting the ECONNRESET error. I’ve created the db connection pool and getting the connection using the pool.getConnection() method. Below is my scenario.

  • I am getting the zip file from the web request.
  • Then reading the file data using fs.readFile() method and trying to insert the file stream data into a table’s column of longblob datatype.
  • I’ve seen no issue when the zip file is below 2 mb to 3 mb size.
  • When it excceds the above size I’m getting this issue.
  • I’ve gone through similar issues reported in github but couldn’t able to fix it.

Stack Trace:

Error: read ECONNRESET
        at exports._errnoException (util.js:1012:11)
        at TCP.onread (net.js:563:26)
    --------------------
        at Protocol._enqueue (app\node_modules\mysql\lib\protocol\Protocol.js:141:48)
        at PoolConnection.query (app\node_modules\mysql\lib\Connection.js:214:25)
        at Query._callback (app\services\basedao.js:85:32)
        at Query.Sequence.end (app\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24)
        atQuery._handleFinalResultPacket(app\node_modules\mysql\lib\protocol\sequences\Query.js:144:8)
        at Query.EofPacket (\node_modules\mysql\lib\protocol\sequences\Query.js:128:8)
        at Protocol._parsePacket (app\node_modules\mysql\lib\protocol\Protocol.js:280:23)
        at Parser.write (app\node_modules\mysql\lib\protocol\Parser.js:74:12)
        at Protocol.write (app\node_modules\mysql\lib\protocol\Protocol.js:39:16)
        at Socket.<anonymous> (E:\app\node_modules\mysql\lib\Connection.js:109:28)
        at emitOne (events.js:96:13)
        at Socket.emit (events.js:188:7)
        at readableAddChunk (_stream_readable.js:177:18)
        at Socket.Readable.push (_stream_readable.js:135:10)
        at TCP.onread (net.js:542:20)

My code:

var pool = mysql.createPool({
    connectionLimit: 10,
    host: process.env.HOSTNAME,
    port: process.env.PORT,
    user: process.env.USERNAME,
    password: process.env.PASSWORD,
    database: process.env.NAME,
    debug: false
});

function insertpackage() {
   getdbconnection(function(err, connection){
        if (err) {
            console.log('Failed to get connection');
        }
        else {
                    var sqlQuery = 'INSERT INTO user_packages SET ?';
                   /* fileData is the stream that is read from my zip file */
                    var params = {
                              fileName: name,
                              content: fileData
                         };
                    connection.query(sqlquery, params, function (err, result) {
                      connection.release();
                      if (err) {
                            console.log( 'Error in  executing Insert query :' + err.stack);
                      }
                      else {
                         console.log('Package uploaded successfully');
                     }
                   });
           }
   });
}

function getdbconnection(callback) {
    pool.getConnection(function (err, dbconnection) {
        if (err) {
            console.log('Error in acquiring DB Connection :', err);
            callback(null, null);
        }
        else {
            console.log('Acquired Database Connection with Connection Id as :' + dbconnection.threadId);
            callback(null, dbconnection);
        }
    });
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
bruno147commented, Oct 11, 2017

I am facing this same issue. When the file’s size is greater than 4 mb the ECONNRESET is thrown. I have succesfully inserted the same file straight to the database using MySQL without node, which led me to believe this is a node issue. My code looks like that:

// main 
function (inner_callback) {
        var db = require('../lib/database')
        var file = ... // 14 Mb of base64 encoded file
        var binaryData = new Buffer(file, 'base64');
        var insertData = {
	        'db': 'temporary_files',
        	'json': {
        		... , // lots of smaller fields
	                'CONTEUDO': binaryData
	        }
        };
        db.insertValue(insertData, function(err, insertResult, successCallback) {
	        if (err) {
	        	return successCallback(new Error("File insertion error."));
	        }
                else {
	        	process.nextTick(function(){ successCallback(null); });
                }
        }, inner_callback);
}

// lib/database

exports.insertValue = function(data, callback, successCallback) {
	var sql = "INSERT INTO " + pool.escapeId(data.db) + " SET ?";
	// get a connection from the pool
	pool.getConnection(function(err, connection) {
		if(err) {
			console.log("Insert value -> Get Connection: ");
			console.log(err);
			callback(true, null, successCallback);
			return;
		}
		connection.query(sql, data.json, function(err, results) {
			connection.release();
			if(err) {
				console.log("Insert value -> Insert: ");
				console.log(err);
				callback(true, null, successCallback);
				return;
			}
			callback(false, results, successCallback);
		});
	});
}

And i get:

Insert value -> Insert:
{ [Error: read ECONNRESET]
  code: 'ECONNRESET',
  errno: 'ECONNRESET',
  syscall: 'read',
  fatal: true }
1reaction
edilsonlm217commented, Sep 9, 2020

I’m building an API that track the database’s binlog files and when it reach 4mb “read ECONNRESET” error is thrown.

Any update on it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: read ECONNRESET when inserting data into table
And the SQL request is: INSERT INTO schedules (trainNumber, stopName, baseArrivalTime, baseDepartureTime, beginDate, endDate, updatedOn) VALUES ...
Read more >
Fixing an ECONNRESET error - Postman
The ECONRESET error means that the server unexpectedly closed the connection and the request to the server was not fulfilled. Connection-related ...
Read more >
node-mssql | Microsoft SQL Server client for Node.js
const sql = require('mssql') async () => { try { // make sure that any items are correctly ... or a connection.on is...
Read more >
Error: read ECONNRESET when connected to a mysql server ...
Coding example for the question Error: read ECONNRESET when connected to a mysql server with Node.js-node.js.
Read more >
List of OS error codes | QuestDB: the database for time series
List of OS error codes that may be reported by QuestDB running on Linux and Windows.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found