I am handling a large query of a Salesforce object through SOAP API which takes more than the timeout. Frequent error message is INVALID_SESSION_ID. I have used the queries throughout every code and it is quite cumbersome to parse the exception message on every request that is used in my code. Are there elegant solutions on handling invalid session? Docs still don't answer this question.

I thought of implementing a session renewer where I run a ScheduledThreadExecutor prior to timeout. However, the timeout can be for every 15 minutes (I prefer not to overfit this scenario where the timeout is 120 minutes and I renew the session for every 15 minutes). Is there a way that I can access this info that is present in the Salesforce settings page. Are there any existing session renewer implementations that automatically connect after the error?

  • I don't have the knowledge to answer your question as asked, but is your session timing out during your query (i.e. on the Salesforce side) or during your processing (in Java)? Is it possible to make the query more selective, or to reduce the amount of data that needs to be processed? Session timeout seems like a red flag, and it's probably easier (and better in the long run) to identify and fix issues in your existing code rather than adding new code to work around the timeout.
    – Derek F
    Feb 24 '16 at 17:33
  • @DerekF Unfortunately, my requirement is to query all the records instead of filtering them out.
    – dmachop
    Feb 25 '16 at 14:44

I've taken a slightly different approach to this with the Partner API from .NET. Java is similar enough in the exception handling that the same approach would be possible.

Basically, I wrapped the entire class generated from the Partner WSDL with my own class. Then I overrided every API method that I use. Each and every override can catch and handle the InvalidSession exception, and if the underlying credentials are available, transparently reestablish as session and retry the API call.

This allows me to make whatever call I want to the Partner API and not have to worry about handling expired sessions in the main code.

With .NET I was able to use delegates to do this in a generic way. I have a single method that can make a generic API call while:

  • Handling an expired session
  • Timing the duration of the call
  • Optionally raising an event with the details of the API call.

It does mean that some API calls take slightly longer as the session is reestablished, but it doesn't burn API calls trying to keep it alive. There is no problem handling other scenarios that could expire the session, like explicit logout calls by the same user. I've successfully used a Singleton implementation with the same Salesforce session instance in the 24x7 web application.

  • 1
    Got it Daniel. There's also a way that I found that I could access the session timeout value through developer.salesforce.com/docs/atlas.en-us.api.meta/api/… soapConnection.getUserInfo().getSessionSecondsValid() will return the Session timeout and I could re-establish the connection 5-10 minutes prior to the timeout through ScheduledThreadExecutor. This also conserves the API limit by not over-bearing the session timeouts.
    – dmachop
    Feb 25 '16 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.