Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Any explanation is welcome........

Thanks in advance

With Regards
Deepthi
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic