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
Jon Wu 5Jon Wu 5 

With force-wsc in a web app, how do you safely and efficiently reuse the same connection?

Let's say I have a Java web server application that uses a single user server@example.com to read and write all data from an org. The username / password credentials are part of the server's configuration.

My goal is to "reuse" the same connection info / session info in that I want the best performance possible and in a thread-safe manner.

If for every request, I use all this code https://github.com/forcedotcom/wsc#write-application-code to create a new connection, I think each connection will require a login to get a session ID, before making an actual request. On the other hand, I think sharing the same object from Connector.newConnection(config) would lead to issues if I wanted to different timeouts for various requests or wanted allOrNone set for some requests but not others. I've actually been using this latter method, but now want to vary some of these parameters.

What's the best way to ensure that each SOAP API request can have its own settings like timeout or allOrNone while avoiding extra SOAP API login requests? Am I overthinking this?

One thought I had was to store connection.getConfig().getSessionId() as a static volatile variable, then create each connection with that session ID and a session renewer that would update the global if the session had to be renewed. Is that a good apporach? Is there something simpler?

Thanks!
Chris  ByromChris Byrom
Are you using the Connector and ConnectorConfig objects? It looks like this handles the session management for you.

https://developer.salesforce.com/page/Introduction_to_the_Force.com_Web_Services_Connector  (https://developer.salesforce.com/page/Introduction_to_the_Force.com_Web_Services_Connector )
Jon Wu 5Jon Wu 5
I am using those just like the example in the link you posted. However, my challenge in the web environment is how to best handle things with multiple concurrent requests or threads - all with different settings. If I just use that same code every time, every thread will end up making a separate login request before issuing the desired API calls. If I share the same connection globally, I don't think it's safe to have varying settings in different threads. Is this true?
Jon Wu 5Jon Wu 5
I'll go with making new connections and reusing an existing session ID in Connection.setSessionHeader and ConnectorConfig.

https://twitter.com/diracz/status/765282634905759744