Migrating EJB projects to use optimistic locking

Use the optimistic locking migration plug-in to enable optimistic locking in an EJB project.

The optimistic locking plug-in can be used in the following scenarios:

  • Version 5.6You are migrating to a new version of WebSphere Commerce. EJB projects developed to work with previous version of WebSphere Commerce that contain Container Managed Persistence (CMP) 1.x EJB beans need to be migrated to use WebSphere Commerce optimistic locking. Do this after migrating the EJB project to EJB 2.0 or 2.1.
  • You created new entity beans and want to enable optimistic locking in your new bean.
    Note: You can manually enable optimistic locking. See Creating new entity beans for more information.

About this task

To use WebSphere Commerce optimistic locking, CMP 1.x EJB entity beans must extend from the ECEntityBean class. If the EJB entity bean class defines an ejbLoad method, that method must call super.ejbLoad. If it defines an ejbStore method, that method must call super.ejbStore. Use the WebSphere Commerce optimistic locking validation plug-in for IBM Rational Application Developer to check that the ejbLoad and ejbStore methods of entity beans extend from ECEntityBean or ECAbstractEntityBean

Procedure

  1. Open WebSphere Commerce Developer.
  2. Optional: Validating that CMP entity beans can use optimistic locking
  3. Expand the EJB Project that you want to migrate.
  4. For each ejbCreate method in each class in the project:
    1. Add this.initializeFields(); as the first line in the method.
    2. Add this.initializeOptCounter( primaryKey); as the last line in the method before the return statement, where primaryKey is the key generated by the key class. This method must be called after the primary key is set.

      The resulting method should look similar to the following example, in which memberId is the primary key:

      public com.ibm.commerce.extension.objects.BonusKey ejbCreate( java.lang.Long memberId,java.lang.Integer bonusPoint) 
      throws javax.ejb.CreateException
      {     
           this.initializeFields();
           _initLinks();
           this.memberId=memberId;
           this.bonusPoint=bonusPoint;
           BonusKey myNewBonusKey = new BonusKey (memberId);
           this.initializeOptCounter(myNewBonusKey);
           return null; }
  5. Right-click the EJB Project that you want to migrate.
  6. Select Migrate to WC optimistic locking.
  7. When the migration is complete, click Details. A report of the files migrated displays. Check to see which files were changed, and whether any errors were found. If you need to store the migration results, you can select the results, right-click and select Copy and paste it into a text editor before clicking OK.
  8. Remember to re-deploy the EJB project for the changes to take effect.

Results

The optimistic locking plug-in changes the following files:

File Changed File Type Changes Made
ejb-jar.xml EJB bean definitions A new CMP field named "optCounter" is added, if it does not already exist, to all EJB 1.x entity beans whose primary key starts with "java.lang", or ends with "<class-name>Key", where <class-name> is the name of the EJB 1.x entity bean class.
ibm-ejb-jar-ext.xmi IBM WebSphere EJB bean extensions. For each ejbExtensions element where xmi:type="ejbext:ContainerManagedEntityExtension", the plugin sets concurrencyControl="optimistic" and sets isolationLevel="READ_COMMITTED" on the isolationLevelAttributes element.
Map.mapxmi Mappings from EJB bean attribute definitions to database table schema column definitions For EJB 1.x entity beans, the plugin creates a mapping with optimistic="true", if it doesn't already exist, from the "optCounter" CMP field to the "OPTCOUNTER" table schema column.

When the plug-in detects that a change is required to a file, it writes the information from the original file in a file with the same name in the same directory but with an added extension of ".orig". It then replaces the original file with the migrated information. For example, the original information from the ejb-jar.xml file is written to a file named ejb-jar.xml.orig, and then the migrated information replaces the contents of the ejb-jar.xml file. You can use a file differencing tool to see the changes that were made.