Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble using @Resource and JNDI lookup
    primarykey
    data
    text
    <p>I'm having trouble applying the concept of injection and also JNDI with EJBs and I'd like your help. I am learning this and I really want to understand and apply the techniques of @Resource and/or JNDI lookups with XML configuration. I can't seem to find any of the initial parameters in my JNDI lookups. Now, before I continue, if I manually enter the JNDI name of my datasource, everything works great. What I'm trying to do (again as an exercise) is use @Resource or JNDI to get the JNDI datasource name itself and then do a JNDI lookup on the datasource. I know that you can directly inject a DataSource object but if I can't inject a String, I gotta start with this.</p> <p>First, here's the important part of my ejb-jar.xml:</p> <pre><code>&lt;env-entry&gt; &lt;description&gt;DataSource JNDI lookup name&lt;/description&gt; &lt;env-entry-name&gt;datasourceName&lt;/env-entry-name&gt; &lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt; &lt;env-entry-value&gt;myDataSource&lt;/env-entry-value&gt; &lt;/env-entry&gt; </code></pre> <p>In terms of injection using @Resource I've tried I've tried the following field in the EJB</p> <pre><code>@Resource(name = "datasourceName") String dsName; </code></pre> <p>I've also tried with mappedName and lookup (using JNDI lookup) nothing is coming in. Then I've tried JNDI lookup as follows:</p> <pre><code> // get JNDI name from environment entry in EJB Context and use to lookup DataSource Context context = new InitialContext(); String dsName = (String) context.lookup("java:comp/env/SRS/datasourceName"); </code></pre> <p>(The name of the application is SRS) - This comes up with a NamingException that the JNDI lookup did not find anything. I've tried the following lookups:</p> <pre><code>String datasourceName = (String) context.lookup("java:comp/env/SRS/Status/datasourceName"); // name of EJB is Status String datasourceName = (String) context.lookup("datasourceName"); String datasourceName = (String) context.lookup("SRS/datasourceName"); String datasourceName = (String) context.lookup("SRS/Status/datasourceName"); String datasourceName = (String) context.lookup("java:global/SRS/datasourceName"); String datasourceName = (String) context.lookup("java:global/SRS/Status/datasourceName"); </code></pre> <p>My first post here so I hope I asked properly. Thanks for the help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload