3 Replies Latest reply on Dec 2, 2003 5:01 AM by jonlee

    connection OK in JSP but fail in java!

    bayuehua

      Hi,
      I just config a datasource 'oracle-ds.xml' in my JBoss322, and in a JSP file i can connect it well. But a single java class gives 'javax.naming.NameNotFoundException: OracleDS not bound'. Why?

      thanks in advance...

      my files

      ******************oracle-ds.xml


      <local-tx-datasource>
      <jndi-name>OracleDS</jndi-name>
      <connection-url>jdbc:oracle:thin:@172.20.20.1:1521:oradev</connection-url>
      <!--

      Here are a couple of the possible OCI configurations.
      For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm

      <connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
      or
      <connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>

      Clearly, its better to have TNS set up properly.
      -->
      <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
      <user-name>system</user-name>
      manager
      <!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->
      <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name>
      <!-- Checks the Oracle error codes and messages for fatal errors -->
      <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
      <!-- sql to call when connection is created
      <new-connection-sql>some arbitrary sql</new-connection-sql>
      -->

      <!-- sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
      <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
      -->

      </local-tx-datasource>




      ********************* index.JSP

      <%@page contentType="text/html"%>
      <%@ page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext" %>
      <h3>Test Oracle9i Database</h3>
      <%
      InitialContext ctx = new InitialContext();
      out.println("initailize OK!");
      out.println("");
      DataSource ds = (DataSource) ctx.lookup("java:/OracleDS");
      out.println("look up OK!");
      out.println("");
      Connection conn = ds.getConnection();
      out.println("db connection OK!");
      out.println("");
      Statement stmt = conn.createStatement();
      out.println("create statement OK!");
      out.println("");
      out.println("execture query : SELECT * FROM tab");
      out.println("");
      ResultSet rs = stmt.executeQuery("SELECT * FROM tab"); while ( rs.next() ) {
      out.println(rs.getString("tname"));
      out.println("");
      }
      conn.close();
      %>

      ***********************test.java

      public static void main(String[] args) {
      try {
      // Hashtable env = new Hashtable();
      // env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      // env.put(Context.PROVIDER_URL, "localhost:1099");
      System.setProperty("java.naming.factory.initial",
      "org.jnp.interfaces.NamingContextFactory");
      System.setProperty("java.naming.provider.url",
      "localhost:1099");

      InitialContext initialCtx = new InitialContext();
      DataSource ds= (DataSource) initialCtx.lookup("java:/OracleDS"); //may different between containers
      java.sql.Connection conn=ds.getConnection();
      conn.close();
      } catch(Exception ex) {
      System.out.println(ex.getLocalizedMessage());
      ex.printStackTrace();
      }
      }

        • 1. connection OK in JSP but fail in java!
          jonlee

          Hi,
          I just config a datasource 'oracle-ds.xml' in my JBoss322, and in a JSP file i can connect it well. But a single java class gives 'javax.naming.NameNotFoundException: OracleDS not bound'. Why?

          thanks in advance...

          my files

          ******************oracle-ds.xml


          <local-tx-datasource>
          <jndi-name>OracleDS</jndi-name>
          <connection-url>jdbc:oracle:thin:@172.20.20.1:1521:oradev</connection-url>
          <!--

          Here are a couple of the possible OCI configurations.
          For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm

          <connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
          or
          <connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>

          Clearly, its better to have TNS set up properly.
          -->
          <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
          <user-name>system</user-name>
          manager
          <!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->
          <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name>
          <!-- Checks the Oracle error codes and messages for fatal errors -->
          <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
          <!-- sql to call when connection is created
          <new-connection-sql>some arbitrary sql</new-connection-sql>
          -->

          <!-- sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
          <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
          -->

          </local-tx-datasource>




          ********************* index.JSP

          <%@page contentType="text/html"%>
          <%@ page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext" %>
          <h3>Test Oracle9i Database</h3>
          <%
          InitialContext ctx = new InitialContext();
          out.println("initailize OK!");
          out.println("");
          DataSource ds = (DataSource) ctx.lookup("java:/OracleDS");
          out.println("look up OK!");
          out.println("");
          Connection conn = ds.getConnection();
          out.println("db connection OK!");
          out.println("");
          Statement stmt = conn.createStatement();
          out.println("create statement OK!");
          out.println("");
          out.println("execture query : SELECT * FROM tab");
          out.println("");
          ResultSet rs = stmt.executeQuery("SELECT * FROM tab"); while ( rs.next() ) {
          out.println(rs.getString("tname"));
          out.println("");
          }
          conn.close();
          %>

          ***********************test.java

          public static void main(String[] args) {
          try {
          // Hashtable env = new Hashtable();
          // env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
          // env.put(Context.PROVIDER_URL, "localhost:1099");
          System.setProperty("java.naming.factory.initial",
          "org.jnp.interfaces.NamingContextFactory");
          System.setProperty("java.naming.provider.url",
          "localhost:1099");

          InitialContext initialCtx = new InitialContext();
          DataSource ds= (DataSource) initialCtx.lookup("java:/OracleDS"); //may different between containers
          java.sql.Connection conn=ds.getConnection();
          conn.close();
          } catch(Exception ex) {
          System.out.println(ex.getLocalizedMessage());
          ex.printStackTrace();
          }
          }

          • 2. Re: connection OK in JSP but fail in java!
            bayuehua

            "Another way of looking at it is that the JBoss datasource is provided for the use of J2EE components, not standalone clients. "

            can you give more detail? thanks a lot.

            And more, I'm trying a build a common DBConnection interface for all my application to use, if i can't use it 'outside' the JVM, does it mean i must create a EJB first, and only in this EJB can i connect the datasource?

            thaks again....

            • 3. Re: connection OK in JSP but fail in java!
              jonlee

              JBoss, first and foremost provides an environment for J2EE components. Part of this environment is comprised of datasource resources. Standalone clients that exist outside the JVM in which JBoss resides cannot use the datasources.

              So an EJB running in the JBoss environment can use JBoss datasources. An external client can call the EJB and the EJB can use the datasource on behalf of the external client. The external client cannot use the datasource directly.