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.

Pool query/execute prevents script from exiting

See original GitHub issue

Using this: mysql.js

function connection() {
	try {
		const mysql = require('mysql2');

		const pool = mysql.createPool({
			host: 'localhost',
			user: '',
			password: '',
			database: '',
			timezone: 'utc',
			connectionLimit: 100,
			waitForConnections: true,
			queueLimit: 0
		});

		const promisePool = pool.promise();

		return promisePool;
	} catch (error) {
		return console.log(`Could not connect - ${error}`);
	}
}

const pool = connection();

module.exports = {
	connection: async () => pool.getConnection(),
	execute: (...params) => pool.execute(...params),
	query: (...params) => pool.query(...params),
	escape: (...params) => pool.escape(...params)
};

script.js

async function testPromises() {
	test = [1, 2, 3, 4, 5, 6, 7, 3, 73, 7, 37, 7, 7];
	await db.query('SELECT * FROM test LIMIT 10');

console.log('after query');
}

testPromises();

Script never exits and keeps all records in memory indefinitely, however everything after the query executes too so it doesn’t hang on this query. How can I prevent that? I also tried:

const db = require('./mysql');
async function testPromises() {
	connection = await db.connection();
	let test2 = await connection.query('SELECT * FROM test LIMIT 10');
	connection.release();
	console.log(test2);
}

testPromises();

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
izzymgcommented, Jun 11, 2019

For an HTTP server a common practice is to create a module which initializes the connection pool and exports it when it’s required, making use of Node’s module caching (effectively a singleton pattern). You can hook into something like process.on("SIGINT", pool.end) to try to gracefully quit. Otherwise keep your pool open for the duration of the process’s lifetime and query it as you wish. Mysql2 will tend to hang Node if you don’t release connections as well, so make good use of finally and the query methods.

0reactions
sidorarescommented, Apr 4, 2022

in that case not much you can do to reduce memory pressure. Maybe use separate pool with a small number of max open connections, that way it’ll act as a semaphore reducing the maximum number of the simultaneous requests processing 300mb of data ( and maybe only release back connection when data processing is done, not after the data is received )

Read more comments on GitHub >

github_iconTop Results From Across the Web

QueryExecute - Adobe Support
This function simplifies the query execution in the CFScript block. This function lets you pass unnamed parameters to the query.
Read more >
Database Queries - Learn Modern ColdFusion <CFML> in ...
A query is a request to a database. It returns a CFML query object containing a recordset and other metadata information about the...
Read more >
How to execute large amount of sql queries asynchronous ...
RunspacePool is the way to go here, try this: $AllQueries = @( ... ) $MaxThreads = 5 # Each thread keeps its own...
Read more >
HANA – analyzing runtime dumps - SAP Blogs
Python script. a FSD (FullSystemDump) only includes already existing RTE dumps => resulting in zip file; Q13. How can runtime dumps be ...
Read more >
Article: Database Integration Guide: FAQ 2 - Boomi Community
In the Database Connection component, on the connection pool tab, ... Boomi keeps passing ? into the SQL query instead of the actual ......
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