File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Resource Injection Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Resource Injection Query" Watch "Resource Injection Query" New topic
Author

Resource Injection Query

nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
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?



Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10048
    
163

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 ]

[My Blog] [JavaRanch Journal]
Aditya Vasudeva
Ranch Hand

Joined: May 27, 2008
Posts: 76
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
Marshal

Joined: Jul 20, 2005
Posts: 10048
    
163

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

Joined: May 30, 2006
Posts: 185
Thanks for the clarification Jaikaran,
Can you also clarify this doubt http://www.coderanch.com/t/163750/java-EJB-SCBCD/certification/Querying-Inside-Entities
aze opuiy
Greenhorn

Joined: Dec 03, 2010
Posts: 4
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 : http://www.coderanch.com/t/519214/EJB-JEE/java/Cla...Type-level-resource-annotation

Thank you
 
jQuery in Action, 2nd edition
 
subject: Resource Injection Query