Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by reference problem in EJB's

 
Ashutosh Shinde
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I am passing a Object between two Stateless Session Beans (say A and B). I have printed the hashcode of the Object passed in A before passing the Object and in B after receiving the Object. Surprisingly, i find the values to be different.
I have enabled the pass by reference in the weblogic-ejb-jar.xml using <enable-call-by-reference>True</enable-call-by-reference>.
Because of this discrepancy I find that I am not able to retrieve the values set on the Object by Bean B in Bean A..
What could be going wrong?
Thanks,
Ashutosh
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are u changing the object in B?
 
Ashutosh Shinde
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Infact I am printing the hash code immediately after the Object is received in the Bean B. So, there is no way the Object is being changed.
But, is this behaviour expected?
Ashutosh
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am bit surprised by the behavior..Call by reference is set to true by default, one need not set it explicitly in weblogic-ejb-jar.xml.
Remember that 2 session beans must be in the same server.
Change the value of the element to "true" from "True".. Lemme know the result
 
Ashutosh Shinde
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pradeep,
I have already set the value to "True". It does not help though. Is it that I have to create a EAR file for the Application and only then the Pass-By-Reference works ?!?
Ashutosh
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass by reference only kicks in when the two EJBs (or servlet and ejb) are in the same "deployment unit".
So if you deploy two separate ejb-jars they will not normally pass-by-reference. If you put your ejbs in one jar or assemble the two ejb jars into an enterprise app (ear) then you will get pass-by-reference.
What's going on is:
Each thing you deploy is turned into an enterprise app if it is not already (look in config.xml and you'll see an Application element wrapping each one).
Each application has its own ClassLoader. So if you have two classes (like the Remote interface or some serializable parameter object) there will actually be two versions of these classes loaded - one by each ClassLoader.
Now when WLS goes to make a "local" (same VM) call on a Remote interface, it checks if both ends know about the same classes. If not, it has to Serialize no matter the setting of pass-by-reference.
If it did not, you would get ClassCastExceptions because even though the classes have the identical name, the instance of the class object loaded by different ClassLoaders is different, thus they are actually different classes to the VM.
So the two ways to fix this are:
Deploy everything you want to pass-by-reference in the same unit - assemble into ear or into one ejb-jar.
Or put all your classes in the system CLASSPATH. This defeats hot-deploy, and goes against the spirit of J2EE and deployable components - so I don't recommend it. But since all classes will be loaded by the system ClassLoader, WLS will see that serialization is not necessary.

The exact same conditions apply to EJB Local interfaces - they can only be used within the same application.
 
Ashutosh Shinde
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave. You made it very clear!
I think we would stick to creating an EAR instead of modifying the System classpath.
Thanks again .
Ashutosh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic