1 2 Previous Next 20 Replies Latest reply on Mar 8, 2002 12:29 AM by luckystar_007

    Help for lookup Datasource

    shink

      Hi,
      I have got the following message in my Jboss startup.
      ....
      [JdbcProvider] Starting
      [JdbcProvider] Started
      [HypersonicDatabase] Starting
      [Default] Server.run/init: java.net.BindException: Address in use: JVM_Bind
      [HypersonicDatabase] Database started
      [HypersonicDatabase] Started
      [XADataSourceLoader] Starting
      [SQLServerPool] XA Connection pool SQLServerPool bound to java:/SQLServerPool
      [XADataSourceLoader] Started
      [ServerDataCollector] Starting
      ....

      but when I run the servlet:
      ...
      InitialContext c = new InitialContext();
      datasource = (DataSource)c.lookup("java:/SQLServerPool");
      Connection conn = datasource.getConnection();
      Statement s = conn.createStatement();
      String q = "select FileName from files where id=1";
      ResultSet rs = s.executeQuery(q);
      rs.next();
      String ff =rs.getString(1);
      out.println(ff);
      ....

      The web error with:
      javax.naming.NameNotFoundException: Name SQLServerPool is not bound in this Context

      Could anybody tell me why?

        • 1. Re: Help for lookup Datasource
          tclouser

          To use the connection pool ensure you have done the following:

          1) in the web.xml

          <resource-ref>
          The default DS
          <res-ref-name>jdbc/SQLServerPool</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
          </resource-ref>

          2) in the jboss-web.xml

          <resource-ref>
          <res-ref-name>jdbc/SQLServerPool
          </res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <jndi-name>java:/SQLServerPool
          </jndi-name>
          </resource-ref>

          3) in your code

          //obtain the initial JNDI context
          javax.naming.Context ctx = new javax.naming.InitialContext();

          //perform JNDI lookup to obtain database connection factory
          javax.sql.DataSource ds = null;
          ds = (javax.sql.DataSource)ctx.lookup(“java:comp/env/jdbc/SQLServerPool”);

          //invoke factory to obtain resource
          javax.sql.Connection conn = ds.getConnection();

          Good references for this exist see
          - Web Container Configuration: use of jboss-web.xml (part of jboss online manual)
          - The JBoss Book 2.4.x (can be purchased from jboss)
          - The Java Servlet Specification

          HTH,

          TC

          • 2. Re: Help for lookup Datasource
            sachin

            Hi tclouser,
            I think that the mentioned code would not work, coz
            >>>>>ds = (javax.sql.DataSource)ctx.lookup(“java:comp/env/jdbc/SQLServerPool”);

            says that the jndi name is java:comp/env/jdbc/SQLServerPool.

            Whereas the web.xml and jboss-web.xml says it is 'java/SQLServerPool'. (infact should be java:/SQLServerPool.

            java:/comp/..... is the way sun's j2ee server wants jndi naming.

            Well, i might be wrong but what do u say?

            Sachin

            • 3. Re: Help for lookup Datasource
              tclouser

              From the JBoss Book 2.4.x (which you can by from jboss.org - see documentation section)

              ...The application component environment is sometimes referred to as the enterprise naming context (ENC). It is the responsibility of the application component container to make an ENC available to the container components in the form of JNDI Context.

              also

              ...JDBC DataSource references should be declared in the java:comp/env/jdbc subcontext.

              Yes, this is a J2EE specification. Yes, it appears that JBoss, despite its lack of certification, is at least J2EE compliant in this area (usage of JNDI contexts).

              What the web.xml states is a declaration of an external resource known as "jdbc/SQLServerPool".

              The jboss-web.xml (which is the vendor specific deployer for web applications), maps the external resource name "jdbc/SQLServerPool" to "java:/SQLServerPool", which is its deployment defined name for this resource. In essence it binds "jdbc/SQLServerPool" to "java:/SQLServerPool".

              Its up to the vendor to implement the access to the defined external resource using the standard java:comp/env/jdbc subcontext for a resource that is of type DataSource.

              HTH

              TC

              PS Each vendor is going to have their own vendor specific deployment descriptor content. But all J2EE compliant server's should allow you to access a DataSource factory using the java:comp/env/jdbc subcontext if you have done the following:

              1. Defined in the web.xml
              2. Mapped in the vendor specific web deployment descriptor

              This is what allows your J2EE compliant application to be deployed to different App Servers without recoding.

              • 4. Re: Help for lookup Datasource
                syzero

                Hi tclouser,

                Thanks a lot, I have learn too much from you, I have
                changed my web.xml and jboss-web.xml just as you memtion.
                But I still get a problem, I know I can lookup the
                context, when I try to cast the object to DataSource,
                I got a error exception :

                java.lang.ClassCastException : org.jboss.naming.java.javaURLContextFactory

                it seems that I cannot cast the context object into DataSource,
                I am sure that the connection pool is started.

                The following is the code at my javabean.
                javax.sql.DataSource ds = (javax.sql.DataSource) cx.lookup("java:comp/env/jdbc/psqlTestDS");

                can you have me to solve this problem? Thanks a lot.

                syzero

                • 5. Re: Help for lookup Datasource
                  sachin

                  Hi tclouser,
                  Yes, you are correct. I knew of the resource reference and actual jndi name mapping. But did not actually read properly the posted message.

                  Thanks for correcting me.
                  Sachin

                  • 6. Can standalone application use connection pool in jboss?
                    ipozeng

                    I have built the conection pool and i am sure it works when using EJB.What i want to know is if i a application can use this pool as below:
                    ....
                    public class DbTest {

                    private static Connection getConnection() throws Exception
                    {
                    Properties h = new Properties();
                    h.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                    h.put(Context.PROVIDER_URL,"localhost:1099");

                    InitialContext initCtx = new InitialContext(h);
                    System.out.println("got context");

                    javax.sql.DataSource ds = (javax.sql.DataSource) initCtx.lookup("jdbc/SQLServerPool");
                    System.out.println("got datasource");

                    initCtx.close();
                    return ds.getConnection();
                    }

                    public static void main(String[] args)
                    {
                    Connection conn;
                    ResultSet rs;
                    PreparedStatement stmt;
                    rs = null;

                    conn = getConnection();

                    ...
                    }

                    Best Regards!

                    • 7. Re: Help for lookup Datasource
                      tclouser

                      syzero,

                      Can you post more of the exception stack trace. Not enough to research the issue.

                      TC

                      • 8. Re: Can standalone application use connection pool in jboss?
                        tclouser

                        IPO zeng,

                        I tried your example without success. What little digging around I did found that the connection pools are not part of the Global JNDI Namespace (goto http://localhost:8082 and click on the service=JNDIView link and then click on the description of list "list button").

                        Its a starting point for further investigation.

                        HTH,

                        TC

                        • 9. Re: Help for lookup Datasource
                          syzero

                          Hi TC,

                          here is the exception stack trace of the casting problem
                          on casting the JNDI context object into DataSource:

                          java.lang.ClassCastException: org.jboss.naming.java.javaURLContextFactory
                          at javax.naming.spi.NamingManager.getURLObjectNamingManager.java:580)
                          at javax.naming.spi.NamingManager.getURLContextNamingManager.java:538)
                          at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:282)
                          at javax.naming.InitialContext.lookupInitialContext.java:354)
                          at javabeans.DataBasePool.getConnectionDataBasePool.java:27)
                          at javabeans.DBBean.getCode(DBBean.java:16)
                          at org.apache.jsp.index$jsp._jspService(index$jsp.java:141)
                          at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java)
                          at javax.servlet.http.HttpServlet.service(HttpServlet.java)
                          at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java)
                          at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java)
                          at org.apache.jasper.servlet.JspServlet.service(JspServlet.java)
                          at javax.servlet.http.HttpServlet.service(HttpServlet.java)
                          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java)
                          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java)
                          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java)
                          at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java)
                          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java)
                          at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java)
                          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
                          at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java)
                          at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java)
                          at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java)
                          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java)
                          at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java)
                          at org.apache.catalina.core.StandardContext.invoke(StandardContext.java)
                          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
                          at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java)
                          at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java)
                          at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java)
                          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java)
                          at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java)
                          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java)
                          at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java)
                          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java)
                          at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java)
                          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java)
                          at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java)
                          at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java)
                          at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java)
                          at java.lang.Thread.run(Thread.java:484)

                          please help me to solve this problem.

                          best regards,
                          syzero

                          • 10. Re: Help for lookup Datasource
                            tclouser

                            syzero,

                            Still not enough information to research the problem. Please post

                            1) the relevant section of your jboss.jcml (where you are declaring the connection pool),
                            2)your web.xml,
                            3)your jboss-web.xml
                            4)and the getConnectionDatabasePool method of DataBasePool.

                            TC

                            • 11. Re: Help for lookup Datasource
                              syzero

                              TC,

                              Thanks your help.

                              -----------------------
                              jboss.jcml


                              org.hsqldb.jdbcDriver,org.postgresql.Driver

                              ...

                              pgsqlDS
                              org.jboss.pool.jdbc.xa.wrapper.XADataSourceImpl
                              jdbc:postgresql://localhost:5432/pgsql
                              postgres
                              postgres


                              ---------------------------------
                              standardjaws.xml

                              java:/pgsqlDS
                              <type-mapping>PostgreSQL</type-mapping>
                              false

                              ---------------------------------
                              web.xml

                              <resource-ref>
                              The postgresql DS
                              <res-ref-name>jdbc/pgsqlDS</res-ref-name>
                              <res-type>javax.sql.DataSource</res-type>
                              <res-auth>Container</res-auth>
                              </resource-ref>

                              ---------------------------------
                              jboss-web.xml

                              <?xml version="1.0" encoding="ISO-8859-1"?>

                              <jboss-web>
                              <resource-ref>
                              <res-ref-name>jdbc/pgsqlDS</res-ref-name>
                              <res-type>javax.sql.DataSource</res-type>
                              <jndi-name>java:/pgsqlDS</jndi-name>
                              </resource-ref>
                              </jboss-web>

                              ---------------------------------
                              public Connection getConnection() {
                              Connection con = null;
                              try {
                              Context cx = getInitialJNDIContext();
                              javax.sql.DataSource ds = (javax.sql.DataSource) cx.lookup("java:comp/env/jdbc/pgsqlDS");
                              con = ds.getConnection();
                              }catch (Exception e) {
                              System.err.println("Unable to retrieve the connection - " + e);
                              }finally {
                              return con;
                              }
                              }

                              private Context getInitialJNDIContext() {
                              Properties props = new Properties();
                              InitialContext ctx = null;
                              try {
                              props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                              props.put(Context.PROVIDER_URL, "localhost:1099");
                              ctx = new InitialContext(props);
                              } catch (Exception e) {
                              System.err.println("Unable to retrieve InitialContext - " + e);
                              } finally {
                              return ctx;
                              }
                              }
                              ------------------------------------
                              Those are all my config and coding involved.

                              syzero

                              • 12. Re: Help for lookup Datasource
                                tclouser

                                syzero,

                                Try the following (modify getConnection and get rid of getInitialJNDIContext()):

                                public Connection getConnection() {
                                Connection con = null;
                                try {
                                Context cx = new InitialContext();
                                javax.sql.DataSource ds = (javax.sql.DataSource)cx.lookup("java:comp/env/jdbc/pgsqlDS");
                                con = ds.getConnection();
                                }catch (Exception e) {
                                System.err.println("Unable to retrieve the connection - " + e);
                                }finally {
                                return con;
                                }
                                }


                                Your code (getInitialJNDIContext) was accessing JNDI in a manner that was not using the container (you only need to do that for an application running outside of the container). I believe the "java:" subcontext is only available to the containers and not to applications accessing the "global" context [but don't quote me on that ;-)].

                                HTH,

                                TC

                                • 13. Re: Can standalone application use connection pool in jboss?
                                  ipozeng

                                  tclouser
                                  Thanks for youe reply first! it is indeed not in Global JNDI Namespace:
                                  ...
                                  java: Namespace
                                  +- jaas (class: javax.naming.Context)
                                  +- TransactionPropagationContextImporter (class: org.jboss.tm.TransactionPropagationContextImporter)
                                  +- JmsXA (class: org.jboss.jms.ra.JmsConnectionFactoryImpl)
                                  +- MinervaSharedLocalCMFactory (class: org.jboss.pool.connector.jboss.MinervaSharedLocalCMFactory)
                                  +- DefaultDS (class: org.jboss.pool.jdbc.xa.XAPoolDataSource)
                                  +- StdJMSPool (class: org.jboss.jms.asf.StdServerSessionPoolFactory)
                                  +- MinervaXACMFactory (class: org.jboss.pool.connector.jboss.MinervaXACMFactory)
                                  +- TransactionManager (class: org.jboss.tm.TxManager)
                                  +- TransactionPropagationContextExporter (class: org.jboss.tm.TransactionPropagationContextFactory)
                                  +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
                                  +- DefaultJMSProvider (class: org.jboss.jms.jndi.JBossMQProvider)
                                  +- SQLServerPool (class: org.jboss.pool.jdbc.xa.XAPoolDataSource) <= here it is
                                  +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
                                  ...
                                  Global JNDI Namespace
                                  +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
                                  +- TopicConnectionFactory[link -> ConnectionFactory] (class: javax.naming.LinkRef)
                                  +- jmx:lbzeng:rmi (class: org.jboss.jmx.server.RMIConnectorImpl)
                                  ...

                                  Best Regards!

                                  • 14. Re: Help for lookup Datasource
                                    shink

                                    I have tried to deploy my web.xml like this :

                                    <?xml version="1.0" encoding="ISO-8859-1"?>

                                    <!DOCTYPE web-app
                                    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
                                    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

                                    <web-app>

                                    <servlet-name>
                                    snoop
                                    </servlet-name>
                                    <servlet-class>
                                    SnoopServlet
                                    </servlet-class>
                                    <!--
                                    <init-param>
                                    <param-name>foo</param-name>
                                    <param-value>bar</param-value>
                                    </init-param>
                                    -->


                                    <servlet-name>
                                    servletToJsp
                                    </servlet-name>
                                    <servlet-class>
                                    servletToJsp
                                    </servlet-class>

                                    <servlet-mapping>
                                    <servlet-name>
                                    snoop
                                    </servlet-name>
                                    <url-pattern>
                                    /snoop
                                    </url-pattern>
                                    </servlet-mapping>
                                    <servlet-mapping>
                                    <servlet-name>
                                    snoop
                                    </servlet-name>
                                    <url-pattern>
                                    *.snp
                                    </url-pattern>
                                    </servlet-mapping>
                                    <servlet-mapping>
                                    <servlet-name>
                                    servletToJsp
                                    </servlet-name>
                                    <url-pattern>
                                    /servletToJsp
                                    </url-pattern>
                                    </servlet-mapping>


                                    <taglib-uri>
                                    http://java.apache.org/tomcat/examples-taglib
                                    </taglib-uri>
                                    <taglib-location>
                                    /WEB-INF/MyTaglib.tld
                                    </taglib-location>


                                    <security-constraint>
                                    <web-resource-collection>
                                    <web-resource-name>Protected Area</web-resource-name>
                                    <!-- Define the context-relative URL(s) to be protected -->
                                    <url-pattern>/jsp/security/protected/*</url-pattern>
                                    <!-- If you list http methods, only those methods are protected -->
                                    <http-method>DELETE</http-method>
                                    <http-method>GET</http-method>
                                    <http-method>POST</http-method>
                                    <http-method>PUT</http-method>
                                    </web-resource-collection>
                                    <auth-constraint>
                                    <!-- Anyone with one of the listed roles may access this area -->
                                    <role-name>tomcat</role-name>
                                    <role-name>role1</role-name>
                                    </auth-constraint>
                                    </security-constraint>

                                    <!-- Default login configuration uses BASIC authentication -->
                                    <!--
                                    <login-config>
                                    <auth-method>BASIC</auth-method>
                                    <realm-name>Example Basic Authentication Area</realm-name>
                                    </login-config>
                                    -->

                                    <!-- Form-based login is enabled by default. If you wish to
                                    try Basic authentication, comment out the <login-config>
                                    section below and uncomment the one above. -->
                                    <login-config>
                                    <auth-method>FORM</auth-method>
                                    <realm-name>Example Form-Based Authentication Area</realm-name>
                                    <form-login-config>
                                    <form-login-page>/jsp/security/login/login.jsp</form-login-page>
                                    <form-error-page>/jsp/security/login/error.jsp</form-error-page>
                                    </form-login-config>
                                    </login-config>

                                    <resource-ref>
                                    The default DS
                                    <res-ref-name>jdbc/SQLServerPool</res-ref-name>
                                    <res-type>javax.sql.DataSource</res-type>
                                    <res-auth>Container</res-auth>
                                    </resource-ref>

                                    </web-app>



                                    But it is error:

                                    Starting service Tomcat-Standalone
                                    Apache Tomcat/4.0
                                    PARSE error at line 102 column -1
                                    org.xml.sax.SAXParseException: org.apache.crimson.parser/V-036 web-app resource-
                                    ref
                                    Starting service Tomcat-Apache
                                    Apache Tomcat/4.0

                                    Why?
                                    Thanks

                                    1 2 Previous Next