Preventing multiple concurrent executions of a WebSphere Commerce command

Technote (troubleshooting)


Problem(Abstract)


This technote describes the necessary steps to prevent multiple concurrent executions of a WebSphere Commerce command.

Even though the WebSphere Commerce scheduler is not initially set up to prevent multiple concurrent executions, the commands can be extended to consider this scenario.

For example, if a command is scheduled to run every five minutes, the scheduler will start a new instance even if the previous one did not complete. In this case, there will be two jobs running the same command at the same time.

If you need to prevent this situation, you can extend your custom commands and ensure the job is not in "R" state on the scheduler tables before invoking the performExecute() method.

This example uses the database as needed in a clustered environment. If your WebSphere Commerce instance is not clustered you have the option to use a static synchronized method instead.

Resolving the problem

To prevent multiple concurrent executions (for example, Singleton):


1. Implement a helper class that provides the "isRunning(String commandName)" method:

import java.util.Vector;
import com.ibm.commerce.base.objects.ServerJDBCHelperAccessBean;

public class SingletonHelper {

public static boolean isRunning( String commandName ) {

Long count = new Long(0);
String query = "SELECT count(*) FROM schconfig, schstatus WHERE schconfig.sccjobrefnum = schstatus.scsjobnbr AND schconfig.sccpathinfo = '" + commandName + "' AND schstatus.scsstate = 'R'";

try {
Vector vecRows = new ServerJDBCHelperAccessBean().executeQuery(query);
if ( vecRows != null ) {
Vector vecRow = (Vector)vecRows.get(0);
if (vecRow != null ) {
count = (Long)vecRow.get(0);
}
}
} catch ( Exception e ) { e.printStackTrace();}

return ( count.longValue() > 1 ) ? true : false ;
}
}

2. Extend the command and invoke the super.performExecute() method only if the command is not running:

public class SingletonControllerCmdImpl extends <Original Command> {
boolean runAlways = false;

public void performExecute() throws ECException {

String commandName = commandContext.getCommandName();

if (runAlways || !SingletonHelper.isRunning(commandName)) {
super.performExecute();
}
}

// add runAlways=true to force execution (bypass singleton)
public void validateParameters() throws ECException {

TypedProperty prop = getRequestProperties();
try {
runAlways = (prop.getBoolean("runAlways"));
} catch (ParameterNotFoundException e) {
runAlways = false;
}
super.validateParameters();
}
}

The "runAlways=true" parameter can be added if you want to force the execution of the command regardless of the number of instances currently running.

3. Register the new class in the CMDREG table, for example:
INSERT INTO CMDREG (STOREENT_ID, INTERFACENAME, DESCRIPTION,CLASSNAME, TARGET)
VALUES ( 0, 'com.ibm.commerce.payment.commands.PaySynchronizePMCmd', 'Singleton implementation of PaySync', 'com.mycompany.commerce.singleton.PaySynchronizePMSingletonCmdImpl', 'Local');

Note: If you are running WebSphere Commerce V5.6 and your Fix Pack is lower than 5, it is recommended that you contact WebSphere Commerce Support and request the following APAR: JR22078. If the system hangs, a scheduled job remains in 'R' state in the SCHACTIVE database table.


Cross reference information
Segment Product Component Platform Version Edition
Commerce WebSphere Commerce Enterprise Customization / Application Development AIX, i5/OS, Linux, Linux pSeries, Linux xSeries, Linux zSeries, Solaris, Windows 6.0 All Editions
Commerce WebSphere Commerce - Express Customization / Application Development AIX, i5/OS, Linux, Linux pSeries, Linux xSeries, Linux zSeries, Solaris, Windows 5.5, 5.6, 5.6.1 Express
Commerce WebSphere Commerce Professional Edition Customization / Application Development AIX, i5/OS, Linux, Linux pSeries, Linux xSeries, Linux zSeries, Solaris, Windows 5.5, 5.6, 5.6.1 Professional Edition

Rate this page:

(0 users)Average rating

Add comments

Document information


More support for:

WebSphere Commerce Business Edition
Customization / Application Development

Software version:

5.5, 5.6, 5.6.1

Operating system(s):

AIX, Linux, Solaris, Windows, i5/OS

Software edition:

Business Edition

Reference #:

1240616

Modified date:

2006-07-05

Translate my page

Machine Translation

Content navigation