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