Restrictions for using the pass-by-reference option for enterprise beans with remote interfaces

According to the Sun Microsystems Enterprise JavaBeans Specification, Version 2.0 the use of the pass by reference option for enterprise beans with remote interfaces is a willful violation of the EJB specification. There are coding considerations that must be kept in mind if this option is chosen to avoid corruption of data.

Object references passed to EJB methods or EJB home methods are not copied and might be subject to corruption. Consider the following example:

Iterator iterator = collection.iterator();                            
MyPrimaryKey pk = new MyPrimaryKey();                                           
while (iterator.hasNext()) {                                    
pk.id = (String) iterator.next();                             
MyEJB myEJB = myEJBHome.findByPrimaryKey(pk);                             
} 

In this code sample a reference to the same MyPrimaryKey object is passed into WAS, each time with a different ID value. If this code is run with the pass-by-reference option enabled, it causes a problem within WAS because multiple enterprise beans are referencing the same MyPrimaryKey object. To avoid this problem, set the system property com.ibm.websphere.ejbcontainer.allowPrimaryKeyMutation to true when running in pass-by-reference mode. This causes the EJB container to make a local copy of the PrimaryKey object. However, a small portion of the performance advantage of setting the pass-by-reference option is lost.

This is one example of problems that might occur as a result of using the pass-by-reference option. As a general rule, any application code that passes an object reference as a parameter to an enterprise bean method or EJB home method must be scrutinized to determine if passing that object reference causes integrity or other problems.