8 Replies Latest reply on Aug 13, 2002 9:22 PM by 807593

    Learning MS JDBC and JNDI Resource

    807593
      Hi all,

      I am trying to learn JDBC and JNDI. I have spent couple days to try to understand these 2 things. I am using:
      Database:MS SQLServer 2000
      JDBC driver: com.microsoft.jdbc.sqlserver.SQLServerDriver
      Tomcat 4.0

      My web.xml

      <web-app>
      <servlet>
      <servlet-name>MyJndi</servlet-name>
      <servlet-class>readjndi.MyJndi</servlet-class>
      </servlet>
      <servlet-mapping>
      <servlet-name>MyJndi</servlet-name>
      <url-pattern>/servlet/MyJndi</url-pattern>
      </servlet-mapping>

      <resource-ref>
      <res-ref-name>jdbc/MyDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>
           
      </web-app>

      My server.xml
      <Context path="/ReadJndi" docBase="ReadJndi" debug="0"
      reloadable="true" crossContext="true">
      <Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"/>
      <ResourceParams name="jdbc/MyDB">
      <parameter>
      <name>user</name>
      <value>sa</value>
      </parameter>
      <parameter>
      <name>password</name>
      <value></value>
      </parameter>
      <parameter><name>driverClassName</name>
      <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value></parameter>
      <parameter>
      <name>driverName</name>
      <value>jdbc:microsoft:sqlserver://localhost:MyDB</value>
      </parameter>
      </ResourceParams>

      </Context>

      My servlet:
      package readjndi;

      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.security.*;
      import java.io.*;
      import javax.sql.*;
      import java.sql.*;
      import javax.naming.*;


      public class MyJndi extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html";
      public void init() throws ServletException {
      DataSource ds = null;
      try {
      ds = (DataSource) (new InitialContext().lookup ("java:comp/env/jdbc/MyDB"));
      }
      catch (Exception e) {
      throw new UnavailableException("Error looking up dataSource" + ": " + e.getMessage());
      }

      }
      }

      I got an error msg:
      javax.servlet.UnavailableException: Error looking up dataSource: Exception creating DataSource: tyrex/jdbc/xa/EnabledDataSource
           at readjndi.MyJndi.init(MyJndi.java:47)
           at javax.servlet.GenericServlet.init(GenericServlet.java:258)
           at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:918)
           at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:655)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
           at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
           at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
           at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
           at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
           at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
           at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
           at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
           at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
           at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
           at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
           at java.lang.Thread.run(Thread.java:536)

      If I use: ds = (DataSource) (new InitialContext().lookup("jdbc/CaPSURE"));

      I got a different error msg:
      javax.servlet.UnavailableException: Error looking up dataSource: Name jdbc is not bound in this Context
           at readjndi.MyJndi.init(MyJndi.java:47)
           at javax.servlet.GenericServlet.init(GenericServlet.java:258)
           at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:918)
           at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:655)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
           at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
           at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
           at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
           at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
           at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
           at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
           at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
           at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
           at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
           at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
           at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
           at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
           at java.lang.Thread.run(Thread.java:536)

      Can anyone point out the mistake for me?

      thanks in advance

      steve
        • 1. Re: Learning MS JDBC and JNDI Resource
          807593
          Try this
          Context env = (Context) new InitialContext().lookup("java:comp/env");
          ds = (DataSource) env.lookup ("jdbc/MyDB"));
          Dave

          • 2. Re: Learning MS JDBC and JNDI Resource
            807593
            thank you Dave,

            However, I still got the same error msg (the 1st one). In addition, at the Tomcat (Dos window) , I found addition error msg:

            Typex.datasourceFactory: Cannot create DataSource, Exception java.lang.NoClassDefFoundError: tyrex/jdbc/xa/EnableDataSource
            at org.appache.naming.factory.TypexDataSourceFactory.getObjectInstance<TyprexDataSourceFactory.java:163>
            at org.apache.naming.factory.TyrexDataSourceFactory.getObjectInstance<NamingManager.java:301>
            ........

            I am so frustrated.
            Anyone has any idea about this error.

            thanks

            steve
            • 3. Re: Learning MS JDBC and JNDI Resource
              807593
              Tomcat uses the Tyrex package to provide DataSource pooling. As it can't find the Tyrex DataSource class you either don't have the tyrex jar in your classpath or have an incorrect one.
              Check if the tyrex jar is in %CATALINA_HOME%\common\lib.
              For example in my installation (4.0.1) I have the file tyrex-0.9.7.0.jar.


              Dave
              • 4. Re: Learning MS JDBC and JNDI Resource
                807593
                thanks Dave,

                I use Tomcat 4.0.4 and when I check the common\bin I did not see the typrex..jar. So I downloaded the tyrex-1.0.jar and put it in the common\bin directory. I also create CLASSPATH=%CATALINA_HOME%\common\lib. One again , I still get the same error NoClassDefFoundError.

                TyprexDataSourceFactory: Cannot create DataSource, Exception java.lang.NoClassDefFoundError: typrex/jdbc/xa/EnabledDataSource
                .....

                Did I miss something? Do I have to create a Typrex Data source factory in the web.xml file?

                steve
                • 5. Re: Learning MS JDBC and JNDI Resource
                  807593
                  I find the best way to handle JNDI issues in these cases is to create a JSP that simply shows you a tree of the names in the JNDI environment.

                  the tomcat config for JNDI can be troublesome having a "JNDI browser" can speed up much debuging
                  • 6. Re: Learning MS JDBC and JNDI Resource
                    807593
                    There's no need to add to your CLASSAPATH Tomcat handles all that if you have your jars in the usual places.

                    If you examine your second error message you'll see that the Tyrex Factory is being called and is the one that raises the error.

                    Check that the tyrex jar contains the class file for tyrex.jdbc.xa.EnabledDataSource. If it doesn't then download the one that I mentioned earlier.

                    You have misspelled Tyrex (Typrex) in all of your posts. Is this because you have typed them in?

                    Dave
                    • 7. Re: Learning MS JDBC and JNDI Resource
                      807593
                      thanks for the info.

                      I found out the tyrex-1.0.jar contains tyrex.resource.jdbc.xa.EnableDataSource but the Tomcat (Dos window) is looking for tyrex.jdbc.xa.EnableDataSource. So I guess I got the wrong version of typrex. When I went to http://tyrex.exolab.org/ , I didn't see any other versions other than the one I got tyrex-1.0.

                      does anyone know where I can get the older version of tyrex or a way to get around the error I got?

                      thanks

                      steve
                      • 8. Re: Learning MS JDBC and JNDI Resource
                        807593
                        Finally, I got it. I use Google and search for tyrex-0.9.7.0.jar and I found a link to download that file. The program is working now.

                        thanks Dave and everyone.

                        Steve