Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Doubt No 2 on Sun Assesment

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Goutham,

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

Thanks
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :

(DataSource) ctx.lookup("jdbc/BarDB")


this becomes a valid lookup.

Hope this is useful to you.

Regards,
Smruti
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


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?
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!