+ Start a Discussion

Random "INVALID_SESSION_ID" errors during batch job

Hi there,


Any help is greatly appreciated. 


I have a scheduled .net program which queries SF objects for reporting, eg. getting contacts, opportunties related information from all accounts in the system.  This report (when successful) runs for 2-3 hours.  I have read other discussion board postings on session timeouts so I made sure that I am logging in and out of Salesforce during the reporting process so that no single session exists for more than 2 hours (note: my organization's session timeout is set to 8 hours anyway).


The problem here is that I cannot consistently run through the entire report.  Given the *same* .net codebase, sometimes it runs for an hour or so before it throws the INVALID_SESSION_ID error, sometimes it runs for > 2 hours, sometimes < 1 hour, sometimes it runs through completely.


My login code is very simple:


    class Program
        public static SforceService sfdc = new SforceService();






                LoginResult lr = sfdc.login("user@company.com", "Hello123");

                if (!lr.passwordExpired)
                    sfdc.Url = lr.serverUrl;
                    sfdc.SessionHeaderValue = new SessionHeader();
                    sfdc.SessionHeaderValue.sessionId = lr.sessionId;
                    writeDebug("Current Session ID: " + lr.sessionId);

                    writeDebug("Your password has expired.\n");

                writeDebug("[INFO] Logged in to Salesforce at " + DateTime.Now.ToString());



As you can see, I am printing out the session key before I set it to the session header and login.  Here is an example of a failed run.  As you can see, the invalid session key in the exception message matches the session key that is set before the login so I am not sure why it would be invalid unless somehow Salesforce dropped or invalidated the session unexpectedly.


 Current Session ID: 433200D00000000hgkJ!ARgAQJ4rsOqnp_kj3BkvtsT_nhJ2y18YD0XOoFe9bLpGhXJXONX_z440EOMdWc03iApAWNi3UfEBVl6CUG61sgOQ7_YyvD1y
[INFO] Logged in to Salesforce at 3/3/2009 1:57:35 AM


.... after processing a bunch of queries (any number of them, does not always fail at the same spot)


[ERROR] 3/3/2009 2:03:24 AM: Error executing SOQL.
INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session key: 433200D00000000hgkJ!ARgAQJ4rsOqnp_kj3BkvtsT_nhJ2y18YD0XOoFe9bLpGhXJXONX_z440EOMdWc03iApAWNi3UfEBVl6CUG61sgOQ7_YyvD1y

Due to the random nature of this error, I cannot tell when the report will or will not run successfully.   How can I tell why a session is invalidated by Salesforce?  And what can I do in this case to obtain a more stable session?  Is this a SF bug?


I am using the partner WSDL dated Dec 2008.


Thank you in advance for the help,


Message Edited by Iceman123 on 03-03-2009 04:30 PM
Best Answer chosen by Admin (Salesforce Developers) 

I ended up working around this by implementing code to relogin and reset the session every time SF loses the session unexpectedly (not due to timeout).


All Answers


I ended up working around this by implementing code to relogin and reset the session every time SF loses the session unexpectedly (not due to timeout).


This was selected as the best answer

Hi Iceman,

I am also running into the above exception exception randomnly..I am setting the timeout value to make sure

that the connection doesn't break during the batch execution...Also to be on the safer end for each set of operation i am establishing new connection and once the task is done explicitly log out.

Could you please let me kow how do you establish connection with SFDC everytime you get into Invalid Sesion Exception?


I perform the following:

1.  Catch exception in my query function (the one that ends up calling SForceService.query) and if the exception contains INVALID_SESSION_ID, I know it is one of these intermittent login issues because my login user is always the same and the password does not change.

2.  In the catch code, I issue a login again for the SForceService.

3.  Then I use goto for it to re-run the query code in my query function: http://msdn.microsoft.com/en-us/library/13940fs2(VS.71).aspx