Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand @Resource mappedName element at all

I've read both and I don't understand them, can someone give an example and explain it in plain english please ? mainly what's the difference between it and the "name" element/

From Oracle tutorial

The mappedName element is a non-portable, implementation-specific name that the resource should be mapped to. Because the name element, when specified or defaulted, is local only to the application, many Java EE servers provide a way of referring to resources across the application server. This is done by setting the mappedName element. Use of the mappedName element is non-portable across Java EE server implementations.

From Oracle Javadocs mappedName

public abstract String mappedName

A product specific name that this resource should be mapped to. The name of this resource, as defined by the name element or defaulted, is a name that is local to the application component using the resource. (It's a name in the JNDI java:comp/env namespace.) Many application servers provide a way to map these local names to names of resources known to the application server. This mapped name is often a global JNDI name, but may be a name of any form. Application servers are not required to support any particular form or type of mapped name, nor the ability to use mapped names. The mapped name is product-dependent and often installation-dependent. No use of a mapped name is portable.

Default: ""

like image 675
Ismail Marmoush Avatar asked Dec 20 '22 00:12

Ismail Marmoush


1 Answers

Say you have a connection pool (javax.sql.DataSource) configured in the application server and placed in JNDI under e.g. java:datasources/jta/MyDb.

Say a component (an EJB probably) of your application needs to access a database. The DataSource could be injected as:

@Resource
private DataSource theDatabase;

Your component may be generic, i.e. can be deployed in multiple environments. Or in a single environment there may be many datasources (java:datasources/jta/MyDb1, java:datasources/jta/MyDb2, ...). How do you map the specific DataSource to your component?

@Resource(mappedName="java:datasources/jta/MyDb")
private DataSource theDatabase;

(Exact details could be missing or be application-server specific, but I hope you get the general point.)

like image 69
Nikos Paraskevopoulos Avatar answered Dec 28 '22 07:12

Nikos Paraskevopoulos