jsforce icon indicating copy to clipboard operation
jsforce copied to clipboard

Error: INVALID_SESSION_ID

Open hden opened this issue 7 years ago • 4 comments

Context

I was trying to create a connection manager for a cloud function. Since it's estimated that the function will be will be invoked in a very low frequency (e.g. few times in a day) so keeping a connection alive did not seems to be worthwhile. When I use the following wrapper to ensure that connections are always properly closed, the error came up.

const jsforce = require('jsforce')
const exec = async (fn) => {
  debug('connecting')
  const conn = new jsforce.Connection({
    loginUrl: process.env.SALESFORCE_URL
  })
  await conn.login(process.env.SALESFORCE_USERNAME, process.env.SALESFORCE_PASSWORD)
  try {
    await fn(conn)
  } catch (e) {
    reportError(e)
  } finally {
    debug('disconnecting')
    conn.logout().catch(reportError)
  }
}

Error message.

Error: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session.
Session not found, missing session hash: <redacted>.
This error usually occurs after a session expires or a user logs out. 

Maybe it's answered in #210, but what's the best practice for managing connection in a long-running process?

  1. Globally shared singleton.
  2. Reconnect every time.
  3. Connection pool.

Environments:

  • Node.js v10 (Google Cloud Functions)
  • jsforce v1.9.1

hden avatar Dec 20 '18 06:12 hden

Make sure process.env.SALESFORCE_PASSWORD has your security token appended to it?

jsullivanlive avatar Dec 21 '18 22:12 jsullivanlive

Late to the party but +1 to this question. (Also I'm aware that one needs to append the security token.)

@hden did you ever figure out a practice that works well?

techieshark avatar Jan 15 '23 01:01 techieshark

No, but I found a Japanese article suggesting that a global shared singleton should be used.

https://kikutaro777.hatenablog.com/entry/2016/03/19/232116

hden avatar Jan 15 '23 02:01 hden

Thanks @hden! :)

techieshark avatar Jan 15 '23 09:01 techieshark