function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AcademicHoboAcademicHobo 

How the Session Id Works in the Web Service API

We set up an application which did a salesforce login followed by an upsert and a logout.  What we learned was that if we had multiple threads using this application, once the first thread reached the logout step, the other threads' session ids became invalid.  We got around the problem by dropping the logout step.  My question is this:

 

1.  If two threads login with the same credentials, do they get the same sessionId?

2.  If so, is it then just best practice not to logout and instead let the session timeout?  Each thread would still log in.

3.  Or would it be better practice to store the session Id after one login and have other threads try to access the salesforce web service using that sessionId?  Only if the access fails would the thread log in.

 

Thx.

 

- Cris

AcademicHoboAcademicHobo
OK....a colleague discovered the answer to question 1 in the Salesforce documentation.  So two threads logging in with the same creds DO share the session id.  But my follow-up question remains.  What is the best way for multiple threads to share the session id without inadvertantly disabling any of them?  My inclination here is to simply log in with each thread and never log out.  Is there a better way?
marioluisscmarioluissc

HI, 

 

I wanna know the same thing.

Do you get reply about your questions?

 

Thanks,

SuperfellSuperfell

Don't call logout, also call login once from one thread and share the resulting sessionId value, there's no need to call login from each thread.

AcademicHoboAcademicHobo

Yes...I learned that the hard way.  Especially the second part of that advice.  While it is true that consecutive logins without logouts return the same session id, you could face a problem if that's all you do.  Too many logins in too short a period and SF will start rejecting them.  I had this experience with high load.  Now, I salt away the session id in a database and try it first.  I only login if the session id is rejected owing to having expired.

 

- Cris