Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The portable approach for defining data sources is to use a <em><strong>resource reference</strong></em>. Resource references enable you to define the JNDI name for your data source, relative to your application naming context (<code>java:comp/env</code>), and then map that <em>logical reference</em> to the <em>physical resource</em> defined in the application server, whose JNDI name is <em>proprietary</em> to the application server vendor. This approach enables your code and assembly to be portable to any compliant application server.</p> <h1>Step 1: Declare and Lookup Resource Reference</h1> <h2>Option 1</h2> <p>This can be done by declaring a <code>resource-ref</code> in your web deployment descriptor (<code>WEB-INF/web.xml</code>):</p> <pre><code>&lt;resource-ref&gt; &lt;description&gt;My Data Source.&lt;/description&gt; &lt;res-ref-name&gt;jdbc/MyDataSource&lt;/res-ref-name&gt; &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt; &lt;res-auth&gt;Container&lt;/res-auth&gt; &lt;/resource-ref&gt; </code></pre> <p>Within your code, you can then lookup this resource using the JNDI name <code>java:comp/env/jdbc/MyDataSource</code>:</p> <pre><code>dataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/MyDataSource"); </code></pre> <p>This JNDI name will not change regardless of the server where the application is deployed.</p> <h2>Option 2</h2> <p>Alternatively, starting in Java EE 5 (Servlet 2.5), this can be done even easier within your code using the <a href="http://download.oracle.com/javaee/5/api/javax/annotation/Resource.html" rel="noreferrer"><code>@Resource</code></a> annotation. This eliminates the need for configuring the resource-ref in your web deployment descriptor (web.xml) and prevents the need to perform an explicit JNDI lookup:</p> <pre><code>public class MyServlet extends HttpServlet { @Resource(name = "jdbc/MyDataSource") private DataSource dataSource; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // dataSource may be accessed directly here since the container will automatically // inject an instance of the data source when the servlet is initialized } </code></pre> <p>This approach has the same results as the previous option, but cuts down on the boilerplate code and configuration in your assembly.</p> <h1>Step 2: Map Resource Reference to Data Source</h1> <p>Then, you will need to use your application server's proprietary approach for mapping the resource reference to the <em>physical data source</em> that you created on the server, for example, using JBoss's custom deployment descriptors (<code>WEB-INF/jboss-web.xml</code>):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;jboss-web&gt; &lt;resource-ref&gt; &lt;res-ref-name&gt;jdbc/MyDataSource&lt;/res-ref-name&gt; &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt; &lt;jndi-name&gt;java:/MyDataSource&lt;/jndi-name&gt; &lt;/resource-ref&gt; &lt;/jboss-web&gt; </code></pre> <p>Or, for example, using Tomcat's <code>context.xml</code>:</p> <pre><code>&lt;Resource name="jdbc/MyDataSource" . . . /&gt; </code></pre>
 

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