About Question enthuware.oce-ejbd.v6.2.419 :

Moderator: admin

Post Reply
sanju.ait@gmail.com
Posts: 38
Joined: Fri Aug 16, 2013 11:37 pm
Contact:

About Question enthuware.oce-ejbd.v6.2.419 :

Post by sanju.ait@gmail.com »

The name() attribute refers to what the JNDI ENC name is for the referenced external
resource. This name is relative to the java:comp/env context.

By providing name only in resource, ConnectionFactory won't be populated. Name is resource is for further reference in the bean itself.

To get the reference "jms/myTCF" need to be in looked up, otherwise won't get the reference.

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by alex »

Could you please explain difference between @Resource(name="jms/myTCF") and @Resource(lookup="jms/myTCF").
And why @Resource(lookup="jms/myTCF") is not correct.

I am playing with java EE 7 examples (GlassFish4) and they use @Resource(lookup) everywhere. And as I see there are relative paths.

@Resource(lookup = "jms/DurableConnectionFactory")
private static ConnectionFactory durableConnectionFactory;
@Resource(lookup = "jms/MyTopic")
private static Topic topic;

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by admin »

The main difference is lookup is used when the name already exists in the name space, while name is used when you are associating a resource with a name.

Regarding relative paths, I am not sure why it is working on Glassfish, but the JavaDoc here is quite clear that it uses global JNDI name: http://docs.oracle.com/javase/7/docs/ap ... tml#lookup()

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

___wst
Posts: 3
Joined: Fri Feb 14, 2014 4:57 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by ___wst »

Hello. First two possible answers are:

Code: Select all

Context initialContext = new InitialContext();
ConnectionFactory myTCF = (ConnectionFactory) initialContext.lookup("java:comp/env/jms/myTCF");

Code: Select all

InitialContext initialContext = new Context();
ConnectionFactory myTCF = (ConnectionFactory) initialContext.lookup("java:comp/env/jms/myTCF");
Could you please say what's the difference is and why first is right while second is not?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by admin »

First one is doing new InitialContext, while second one is doing new Context. InitialContext is a class. Context is an interface.
If you like our products and services, please help us by posting your review here.

___wst
Posts: 3
Joined: Fri Feb 14, 2014 4:57 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by ___wst »

Yes, but question is: Which of the following options correctly acquires a reference to the factory?
What do you mean by "correctly"? Taking design matters apart, wouldn't both of them work fine when placed in code?

What I'm trying to say, I get the feeling that question is not clear enough to instantly exclude second option.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by admin »

Second option has a compilation error because it is doing new Context(); You can't instantiate an interface. It cannot be compiled and so it is not a valid option.

I hope it is clear now.

-Paul.
If you like our products and services, please help us by posting your review here.

___wst
Posts: 3
Joined: Fri Feb 14, 2014 4:57 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by ___wst »

Indeed... I missed that one. I was so focused on left side's object type, that I completely missed what is on the right side. Good lesson - you have to read carefully entire question and answers.

Thank you.

Ciprian Mihalache
Posts: 51
Joined: Wed Sep 28, 2011 12:14 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by Ciprian Mihalache »

I am still not convinced that the last option is incorrect.
I understand that javadoc specifies the "the global JNDI names" but the Oracle Java EE 6 tutorial uses relative JNDI names.
Also, on a quick search, I found 2 books: Beginning Java EE 7 and The Java EE 6 Tutorial: Advanced Topics that are using also relative JNDI names.
The fact that variable is static, has anything to do with the validity of the result?
I understand that the fact it works on some application servers does not mean the specifications dictates that, but in case a similar option appears on a question in the exam, what should I do?
Thank you

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by admin »

It is possible that the JavaDoc comment may be interpreted differently. It says, " It can link to any compatible resource using the global JNDI names." This could also imply that it normally uses relative names but it can use global names as well.
Since multiple books and oracle tutorial is interpreting it this way, it is better to go with that. I will update the question.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ciprian Mihalache
Posts: 51
Joined: Wed Sep 28, 2011 12:14 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by Ciprian Mihalache »

Your explanation seems reasonable. Thank you :)

PS: I just discovered another question that has a similar option: enthuware.oce-ejbd.v6.2.420

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by admin »

Updated here as well.
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by himaiMinh »

Maybe, this is out of the scope of this question.
Session 21.3.2 JNDI Requirements: All EJB container must make available at least the following objects in the name space:
- The local business interface of other EJB ....
- EJBContext objects
- the resource factories used by the EJB ...

Does it mean without lookup or name attribute, we can inject a ConnectionFactory like this:

Code: Select all

@Resource
private ConnectionFactory connectionFactory;
?

We can inject EJBContext or UserTransaction or other EJB like this without any name/lookup attributes in @Resource:

Code: Select all

  @Resource
   EJBContext context;
 
  @UserTransaction
   UserTransaction ut;

  @EJB  //assume MyRemoteRef is the remote view of a bean in the same module;
   MyRemoteRef  myRemoteBean;

  @EJB  // assume MyLocalBean is the no-interface view of a bean in the same module;
   MyLocalBean myLocalBean;

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.419 :

Post by admin »

Yes, the container tries to inject appropriate object based on name and/or type.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests