Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6531695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:54:35+00:00 2026-05-25T09:54:35+00:00

I have DataSource which is configured on Tomcat 6 in context.xml as MyDataSource. And

  • 0

I have DataSource which is configured on Tomcat 6 in context.xml as MyDataSource.
And I’m fetching it the following way:

      DataSource dataSource;
            try {
                dataSource = (DataSource) new InitialContext().lookup("java:comp/env/MyDataSource");
            } catch (NamingException e) {
                throw new DaoConfigurationException(
                    "DataSource '" + url + "' is missing in JNDI.", e);
            }

Everything works fine. Now I’m exporting this code to Jboss AP 6. and I configured my dataSource and its connection pool as local-tx dataSource under the same name.

When I’m executing the code above, I’m getting NamingException exception. after some investigation I’ve found that correct way to call my DataSource under Jboss is

 dataSource = (DataSource) new InitialContext().lookup("java:/MyDataSource");

Can anybody explain me why should I omit “comp/env” in my JNDI path under Jboss?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:54:35+00:00Added an answer on May 25, 2026 at 9:54 am

    The portable approach for defining data sources is to use a resource reference. Resource references enable you to define the JNDI name for your data source, relative to your application naming context (java:comp/env), and then map that logical reference to the physical resource defined in the application server, whose JNDI name is proprietary to the application server vendor. This approach enables your code and assembly to be portable to any compliant application server.

    Step 1: Declare and Lookup Resource Reference

    Option 1

    This can be done by declaring a resource-ref in your web deployment descriptor (WEB-INF/web.xml):

    <resource-ref>
        <description>My Data Source.</description>
        <res-ref-name>jdbc/MyDataSource</res-ref-name> 
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    

    Within your code, you can then lookup this resource using the JNDI name java:comp/env/jdbc/MyDataSource:

    dataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/MyDataSource");
    

    This JNDI name will not change regardless of the server where the application is deployed.

    Option 2

    Alternatively, starting in Java EE 5 (Servlet 2.5), this can be done even easier within your code using the @Resource 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:

    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
    
    }
    

    This approach has the same results as the previous option, but cuts down on the boilerplate code and configuration in your assembly.

    Step 2: Map Resource Reference to Data Source

    Then, you will need to use your application server’s proprietary approach for mapping the resource reference to the physical data source that you created on the server, for example, using JBoss’s custom deployment descriptors (WEB-INF/jboss-web.xml):

    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-web>
        <resource-ref>
            <res-ref-name>jdbc/MyDataSource</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <jndi-name>java:/MyDataSource</jndi-name>
        </resource-ref>
    </jboss-web>
    

    Or, for example, using Tomcat’s context.xml:

    <Resource name="jdbc/MyDataSource" . . . />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try the following lookup in my code: Context initCtx = new InitialContext();
I have a GridView which is databound to an XML Datasource. For one of
On JBoss 5.1.0 I have Datasource (PostgreSQL 8.3.11) configured using *-ds.xml (standard jboss DS).
For JAAS authentication I have configured a datasource as follows: <?xml version=1.0 encoding=UTF-8?> <datasources>
I have a Form with a DataGridView which DataSource is a BindingSource to a
I have a listview which has its datasource changed after update of a search
I have a Clear button which sets the gridview datasource to null and binds
Say I have a listbox lb, which I initialize using: lb.DataSource = wordList \\wordList
I currently have a datasource from a client in the form of XML, this
I have a Tomcat 5.5 running which includes a Webapp with the Java Reporting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.