Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Resource Injection Query

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading Mikalai's guide today where I came in across this code snippet:


@Resource(name="jdbc/EmployeeAppDB", type=javax.sql.DataSource)
@Stateless public class EmployeeServiceBean implements EmployeeService {
EJBContext ejbContext;
public void changePhoneNumber(...) {
...
// obtain the initial JNDI context
Context initCtx = new InitialContext();

// perform JNDI lookup to obtain resource manager
// connection factory
javax.sql.DataSource ds = (javax.sql.DataSource) initCtx.lookup("java:comp/env/jdbc/EmployeeAppDB");

// Invoke factory to obtain a connection. The security
// principal is not given, and therefore
// it will be configured by the Deployer.
java.sql.Connection con = ds.getConnection();
...
}
}



I am not clear as to why the annotation in the first line is used:
@Resource(name="jdbc/EmployeeAppDB", type=javax.sql.DataSource)
because anyways the code uses JNDI to get the context. So is the resource injection actually required?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nitin pai:
I am not clear as to why the annotation in the first line is used:
@Resource(name="jdbc/EmployeeAppDB", type=javax.sql.DataSource)
because anyways the code uses JNDI to get the context. So is the resource injection actually required?



Nitin,

The "name" attribute for the @Resource annotation is not used for looking up the resource for injection. Instead, the resource will be bound to the java:comp/env/<the value you specify for the "name"> (in this example, it will be bound to java:comp/env/jdbc/EmployeeAppDB after injection). As you can see, later on in the changePhoneNumber code, the resource is looked up at java:comp/env/jdbc/EmployeeAppDB. If you hadn't used the "name" attribute then this wouldn't have been possible.
[ June 11, 2008: Message edited by: Jaikiran Pai ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a wierd situation. The whole objective of @Resource is to remove JNDI lookups. So using @Resource could directly give you a DataSource.

I think that if the @Resource is present and then you use a JNDI lookup after that, it will work normally but again its useless to go to a place using a shortcut than using the longer route....get what i mean.
 
Jaikiran Pai
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

Originally posted by Aditya Vasudeva:
This is a wierd situation. The whole objective of @Resource is to remove JNDI lookups. So using @Resource could directly give you a DataSource.



You are missing a point here

The @Resource with the "name" attribute IS injecting the datasource into your ENC JNDI (i.e. the java:comp/env ) namespace. This is similar to having resource-ref in your ejb-jar.xml.
 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification Jaikaran,
Can you also clarify this doubt https://coderanch.com/t/163750/java-EJB-SCBCD/certification/Querying-Inside-Entities
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am sorry but i still don't understand.

There are differents posts around this subject so i don't know where to post.
Before i found this topic, I have asked a question on this subject there : https://coderanch.com/t/519214/EJB-JEE/java/Cla...Type-level-resource-annotation

Thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic