4 Replies Latest reply on Feb 26, 2002 4:09 PM by Craig Demyanovich

    Session Bean and JDBC

    Nietsnie Newbie

      I have a stateless session bean that is doing some custom searching.

      When trying to get a database connection from the defaultds I run into a problem.

      Here's the code I'm using:

      InitialContext context = new InitialContext();
      DataSource source = (DataSource)context.lookup("jdbc/DefaultDS");
      return source.getConnection();

      Here's the error I'm getting from JBoss:

      [Default] javax.naming.NameNotFoundException: jdbc not bound

      What's that mean?

      BTW, this code works fine with a different container (OrionServer).


      Jeff Davey

        • 1. Re: Session Bean and JDBC
          Paul Austin Newbie

          try using java:/DefaultDS instead as JBoss binds datasources in the java:/ namespace.


          • 2. Re: Session Bean and JDBC
            Dan Christopherson Newbie

            You really should map the database pool into the beans environment naming context using a resource reference in ejb-jar.xml and jboss.xml. This will allow you to do lookups at 'java:comp/env/jdbc/Whatever' and map that to 'java:/DefaultDS' for JBoss and 'jdbc/DefaultDS' with Orion - this way your code doesn't change, you simply provide different (container-specific) descriptors for different containers.

            • 3. Re: Session Bean and JDBC
              Nietsnie Newbie


              Thanks plenty.

              • 4. Re: Session Bean and JDBC
                Craig Demyanovich Newbie

                My situation is similar to that of the person who started the thread. It differs, however, in that I want to be able to query a different database as circumstances dictate: for each subsidiary at which my component is deployed, the query and the datasource will vary. Declaring a resource reference in the ejb-jar.xml and jboss.xml deployment descriptors per subsidiary is not as dynamic a solution that I intended to create. Must I declare the resource reference, or is there a more dynamic way to accomplish my goal?

                Thanks for your help,