diff --git a/cf/src/connection.js b/cf/src/connection.js index 203af80d..be9c64bf 100644 --- a/cf/src/connection.js +++ b/cf/src/connection.js @@ -82,6 +82,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose , result = new Result() , incoming = Buffer.alloc(0) , needsTypes = options.fetch_types + , typesPromise = null , backendParameters = {} , statements = {} , statementId = Math.random().toString(36).slice(2) @@ -541,12 +542,23 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose if (needsTypes) { initial.reserve && (initial = null) - return fetchArrayTypes() + + // Prevent duplicate requests + needsTypes = false + + // Store the promise so concurrent requests can wait for type initialization to complete + typesPromise = fetchArrayTypes().finally(() => { + typesPromise = null + }) + return } - initial && !initial.reserve && execute(initial) - options.shared.retries = retries = 0 - initial = null + // If there is a pending types request, trigger the execution after it's complete + (typesPromise || Promise.resolve()).then(() => { + initial && !initial.reserve && execute(initial) + options.shared.retries = retries = 0 + initial = null + }) return } diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 589d3638..68ff2f01 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -80,6 +80,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose , result = new Result() , incoming = Buffer.alloc(0) , needsTypes = options.fetch_types + , typesPromise = null , backendParameters = {} , statements = {} , statementId = Math.random().toString(36).slice(2) @@ -539,12 +540,23 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose if (needsTypes) { initial.reserve && (initial = null) - return fetchArrayTypes() + + // Prevent duplicate requests + needsTypes = false + + // Store the promise so concurrent requests can wait for type initialization to complete + typesPromise = fetchArrayTypes().finally(() => { + typesPromise = null + }) + return } - initial && !initial.reserve && execute(initial) - options.shared.retries = retries = 0 - initial = null + // If there is a pending types request, trigger the execution after it's complete + (typesPromise || Promise.resolve()).then(() => { + initial && !initial.reserve && execute(initial) + options.shared.retries = retries = 0 + initial = null + }) return } diff --git a/deno/src/connection.js b/deno/src/connection.js index a3f43c48..c8e317fd 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -83,6 +83,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose , result = new Result() , incoming = Buffer.alloc(0) , needsTypes = options.fetch_types + , typesPromise = null , backendParameters = {} , statements = {} , statementId = Math.random().toString(36).slice(2) @@ -542,12 +543,23 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose if (needsTypes) { initial.reserve && (initial = null) - return fetchArrayTypes() + + // Prevent duplicate requests + needsTypes = false + + // Store the promise so concurrent requests can wait for type initialization to complete + typesPromise = fetchArrayTypes().finally(() => { + typesPromise = null + }) + return } - initial && !initial.reserve && execute(initial) - options.shared.retries = retries = 0 - initial = null + // If there is a pending types request, trigger the execution after it's complete + (typesPromise || Promise.resolve()).then(() => { + initial && !initial.reserve && execute(initial) + options.shared.retries = retries = 0 + initial = null + }) return } diff --git a/src/connection.js b/src/connection.js index c3f554aa..0d59b8aa 100644 --- a/src/connection.js +++ b/src/connection.js @@ -80,6 +80,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose , result = new Result() , incoming = Buffer.alloc(0) , needsTypes = options.fetch_types + , typesPromise = null , backendParameters = {} , statements = {} , statementId = Math.random().toString(36).slice(2) @@ -539,12 +540,23 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose if (needsTypes) { initial.reserve && (initial = null) - return fetchArrayTypes() + + // Prevent duplicate requests + needsTypes = false + + // Store the promise so concurrent requests can wait for type initialization to complete + typesPromise = fetchArrayTypes().finally(() => { + typesPromise = null + }) + return } - initial && !initial.reserve && execute(initial) - options.shared.retries = retries = 0 - initial = null + // If there is a pending types request, trigger the execution after it's complete + (typesPromise || Promise.resolve()).then(() => { + initial && !initial.reserve && execute(initial) + options.shared.retries = retries = 0 + initial = null + }) return }