• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in enthuware com.enthuware.ets.scbcd.v5.2.12 - Objective Bean Environment

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,

I have a doubt with the following question.

Consider the bean code shown in Exhibit. Assuming that there is no deployment descriptor given with the bean, which of the following options can be inserted at //1 without causing any exception?

package com.enthu.ejbplus;

import java.sql.*;
import javax.annotation.Resource;
import javax.ejb.*;
import javax.sql.DataSource;

@Stateless
@Remote(com.enthu.ejbplus.TellerRemote.class)
@Resource(name="jdbc/ejbplus", type=DataSource.class)
public class TellerBean {

@Resource
SessionContext sctx;

public TellerBean() {
}

public double getBalance(int acctid){

// Line 1 : insert code here

}
}


Select 2 options

1) DataSource ds = sctx.lookup("jdbc/ejbplus");
2)Principal p = sctx.getCallerPrincipal();
3)RequestDispatcher rd = sctx.getRequestDispatcher();
4)Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/ejbplus");

The correct answers are: 2 and 4

How can 4 be the correct answer

env entries are configured in the deployment descriptor. Since it does not ship with one, how can the lookup method find "java:comp/env/jdbc/ejbplus" name. Hence it should throw :
javax.naming.NameNotFoundException: No object bound to name java:comp/env/jdbc/ejbplus

Because in the specs it says

"The EJB specification recommends, but does not require, that all references to other enterprise
beans be organized in the ejb subcontext of the bean’s environment (i.e., in the
java:comp/env/ejb JNDI context). Note that enterprise bean references declared by
means of annotations will not, by default, be in any subcontext."


 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any explanation is welcome........

Thanks in advance

With Regards
Deepthi
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepthi Tirunahari wrote:
How can 4 be the correct answer

env entries are configured in the deployment descriptor. Since it does not ship with one, how can the lookup method find "java:comp/env/jdbc/ejbplus" name.



They can also be configured using annotations, as this example does.


The @Resource tells the container to bind a datasource resource in the TellerBean's ENC (i.e. java:comp/env). So the datasource will become available at java:com/env/jdbc/ejbplus. The code can then lookup this datasource using that ENC jndi name. And that's what answer #4 is doing.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic