Salesforce WSC Partner Connection Session Renew when Session Timeout

Implement SessionRenewer

package com.sforce.authentication;

import javax.inject.Inject;
import javax.xml.namespace.QName;

import org.slf4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.sforce.async.AsyncApiException;
import com.sforce.async.BulkConnection;
import com.sforce.soap.partner.Connector;
import com.sforce.soap.partner.PartnerConnection;

 * @author tmichels
public class SalesforceAuthenticationConfigImpl implements SalesforceAuthenticationConfig {

	private static final Logger logger = org.slf4j.LoggerFactory.getLogger(SalesforceAuthenticationConfigImpl.class);
	@Inject private Environment environment;
	private PartnerConnection partnerConnection = null;
	private BulkConnection bulkConnection = null;
	public ConnectorConfig productionConnectionConfig(){
		ConnectorConfig config = new ConnectorConfig();
		return config;
	public PartnerConnection loginToProductionSalesforce() {
    	try {
    		ConnectorConfig config = productionConnectionConfig();
    		config.setSessionRenewer(new productionSessionRenewer());
        	partnerConnection = Connector.newConnection(config);
		} catch (ConnectionException e) {
			logger.error("SalesforceAuthenticationConfigImpl.loginToProductionSalesforce(): " + e.getMessage());
		return partnerConnection;
    public class productionSessionRenewer implements SessionRenewer {
		public SessionRenewalHeader renewSession(ConnectorConfig config) throws ConnectionException {
			ConnectorConfig conConfig = productionConnectionConfig();
			partnerConnection = Connector.newConnection(conConfig);
            SessionRenewalHeader header = new SessionRenewalHeader();
   = new QName("", "SessionHeader");
            header.headerElement = partnerConnection.getSessionHeader();
            return header;

Test SessionRenewer

@ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes={SalesforceAuthenticationConfigImpl.class})
public class SalesforceAuthenticationServiceTest {

	@Inject @Named("loginToProductionSalesforce") PartnerConnection loginToProductionPartner;
	public void testLoginToProductionSalesforce() {
		try {
		} catch (ConnectionException e) {


  1. Rod Prechtl says:

    In all honesty, the problem is always that when trading under such a
    model the traders are infact not being shown the true market
    prices to the currency pairs they may be trading.
    There are a myriad of choices, because search on the Internet will disclose, and they also could be
    quite expensive. For an detailed guide on how to choose the best Automatic
    Forex Trading System to fulfill your financial goals, get my free report: The Truth About
    Forex Trading Systems.

  2. Thank you very much for this enlighting post, I already had a EntepriseConnection configured, and this post explaining how the SessionRenewal in Salesforce works was exactly what I was looking for.

    Good post !

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s