Author
Doubt No 2 on Sun Assesment
Chaminda Amarasinghe
Ranch Hand
Joined: May 17, 2006
Posts: 402
9. The deployment descriptor for the Foo session bean looks similar to this: 10. <enterprise-beans> 11. <session> 12. ... 13. <ejb-name>Foo</ejb-name> 14. <ejb-class>ms.FooBean</ejb-class> 15. ... 16. <resource-ref> 17. <res-ref-name>jdbc/BarDB</res-ref-name> 18. <res-type>javax.sql.DataSource </res-type> 19. <res-auth>Container</res-auth> 20. <res-sharing-scope>Shareable</res-sharing-scope> 21. </resource-ref> 22. ... 23. </session> 24.</enterprise-beans> Assume that a deployer binds this to an actual resource. The implementation of Foo contains a reference to a Context object bound to the variable initCtx and a reference to a SessionContext object bound to the variable ctx. Which statement successfully retrieves the datasource? 1. @Resource DataSource ds; 2. (DataSource ) ctx.lookup("java:comp:env/jdbc/BarDB"); 3. initCtx.lookup("java:comp/env/jdbc/BarDB"); 4. @Resource(name=?jdbc/BarDB?, type=javax.sql.DataSource) Can someone explain this. Correct answer is 3. Thanks
Goutham Pallipati
Greenhorn
Joined: Aug 13, 2008
Posts: 20
its simple resource reference name is not the actual JNDI Name. its just a reference to some object of type javax.sql.Database. This will be bind to a real JNDI Resource in the container specific resource files exp ibm-ejb-jar-bnd.xmi incase of IBM Websphere. We use java :comp/env/ is the convention for getting the resource references from the environment file ejb-jar.xml. Hence answer three is correct. If you are new to EJB before trying assessments I would suggest to do some sample applications. Check the below content from ejb-jar.xml <resource-ref id="ResourceRef_1061917437594"> <description/> <res-ref-name>jdbc/sample</res-ref-name> <res-type>javax.sql.DataSource </res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> from ibm-ejb-jar-bnd.xmi <resRefBindings xmi:id="ResourceRefBinding_1061917437594" jndiName="jdbc/SAMPLE_ThinJDBCDataSource"> <bindingResourceRef href="META-INF/ejb-jar.xml#ResourceRef_1061917437594"/> </resRefBindings>
Chaminda Amarasinghe
Ranch Hand
Joined: May 17, 2006
Posts: 402
Hi Goutham, Thank you very much for your grate explanation. Can you please tell me why 2. is incorrect. Thanks
S Bhanage
Greenhorn
Joined: Oct 28, 2009
Posts: 13
posted Oct 30, 2009 09:14:33
0
Hi,
SessionContext extends EJBContext for Session Beans.
EJBContext interface has a convenience ENC lookup method.
Its lookup method does not throw a checked exception and
it takes a relative name into the ENC instead of the full "java:comp/env" string .
Option 2 uses lookup string as "java:comp:env/jdbc/BarDB" which is not a relative string.
Hence option 2 is incorrect.
If it is modified as :
this becomes a valid lookup.
Hope this is useful to you.
Regards,
Smruti
Marcin Faryna
Greenhorn
Joined: Jul 16, 2009
Posts: 15
Hello, In my opinion there is a mistake in this question:
Is wrong because container uses the name java:comp/env/ms.FooBean/ds which is not bound to any DataSource .
2. (
DataSource ) ctx.lookup("java:comp:env/jdbc/BarDB");
Is wrong because like S Bhanage said wrong name was used.
3.initCtx.lookup("java:comp/env/jdbc/BarDB");
This one is wrong because simply there is nothing like Context Object there is SessionContext obviously and is explicity mentioned
... and a reference to a
SessionContext object bound to the variable ctx
4.Resource(name=?jdbc/BarDB?, type=javax.sql.DataSource)
Which is wrongly formed and no field is annotated.
So all four are wrong what do you think?
SCJP 6, SCBCD 5
subject: Doubt No 2 on Sun Assesment