File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Doubt No 2 on Sun Assesment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Doubt No 2 on Sun Assesment " Watch "Doubt No 2 on Sun Assesment " New topic

Doubt No 2 on Sun Assesment

Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 404
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>

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.

Goutham Pallipati

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">

from ibm-ejb-jar-bnd.xmi
<resRefBindings xmi:id="ResourceRefBinding_1061917437594" jndiName="jdbc/SAMPLE_ThinJDBCDataSource">
<bindingResourceRef href="META-INF/ejb-jar.xml#ResourceRef_1061917437594"/>
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 404
Hi Goutham,

Thank you very much for your grate explanation. Can you please tell me why 2. is incorrect.

S Bhanage

Joined: Oct 28, 2009
Posts: 13

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 :
(DataSource) ctx.lookup("jdbc/BarDB")

this becomes a valid lookup.

Hope this is useful to you.

Marcin Faryna

Joined: Jul 16, 2009
Posts: 15
Hello, In my opinion there is a mistake in this question:

1. @Resource DataSource ds;

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.


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?

I agree. Here's the link:
subject: Doubt No 2 on Sun Assesment
It's not a secret anymore!