1 Reply Latest reply on Apr 11, 2006 4:36 AM by Javier Leyba

    MySQL datasource error

    Javier Leyba Newbie

      Hi

      I've a simple web application in JBoss AS that will need to connect to a MySQL DB then I did in WEB-INF of deployed:

      jboss-web.xml

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



      web.xml
      <resource-ref>
       <description>The default MySQL DS</description>
       <res-ref-name>jdbc/MySQLDB</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       </resource-ref>
      




      Adn in my code I did:

      public static Connection getConnection() throws Exception {
       Connection conn = null;
       Context envContext = null;
      
       logger.info( "NOTIFICATIONS: Trying to access to Connection");
      
       try {
       Context initContext = new InitialContext();
       envContext = (Context) initContext.lookup(DBConnection.LOOKUP_LABEL);
       } catch (NamingException ne) {
       logger.error( "NOTIFICATIONS: Error ne en LOOKUP al intentar conseguir conectar al DB pool: " + ne.getMessage());
       } catch (Exception e) {
       logger.error( "NOTIFICATIONS: Error e en LOOKUP al intentar conseguir conectar al DB pool: " + e.getMessage());
       }
      
       try {
       DataSource ds = (DataSource) envContext.lookup(DBConnection.DATASOURCE_LABEL);
       conn = ds.getConnection();
       } catch (NamingException ne) {
       logger.error( "NOTIFICATIONS: Error ne en DATASOURCE al intentar conseguir conectar al DB pool: " + ne.getMessage());
       } catch (NullPointerException npe) {
       logger.error( "NOTIFICATIONS: Error npe en DATASOURCE al intentar conseguir conectar al DB pool: " + npe.getMessage());
       } catch (SQLException sqle) {
       logger.error( "NOTIFICATIONS: Error sqle en DATASOURCE al intentar conseguir conectar al DB pool: " + sqle.getMessage());
       } catch (Exception e) {
       logger.error( "NOTIFICATIONS: Error en DATASOURCE al intentar conseguir conectar al DB pool: " + e.getMessage());
       }
      




      But when my app try to connect I got the message:

      12:35:06,950 INFO [DBConnection] NOTIFICATIONS: Trying to access to Connection
      12:35:06,955 ERROR [DBConnection] NOTIFICATIONS: Error ne en DATASOURCE al intentar conseguir conectar al DB pool: Could not dereference object
      



      Could somebody tell me what am I doing wrong ?


      Thanks in advance

      J



        • 1. Re: MySQL datasource error
          Javier Leyba Newbie

          Please, could somebody tell me how to use a DB connection pool from a web application inside JBoss.

          I've tried everything including reinstalling and configuring JBoss step by step.

          I've deleted all other applications and configured a little one that makes:

          
          public class MyTest extends HttpServlet {
          
           public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException {
           doPost(request, response);
           }
          
           public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws ServletException {
           try {
           InitialContext ctx = new InitialContext();
          
           /*
           * Lookup the DataSource, which will be backed by a pool
           * that the application server provides. DataSource instances
           * are also a good candidate for caching as an instance
           * variable, as JNDI lookups can be expensive as well.
           */
          
           DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MySQLDB");
          
           /*
           * The following code is what would actually be in your
           * Servlet, JSP or EJB 'service' method...where you need
           * to work with a JDBC connection.
           */
          
           Connection conn = null;
           Statement stmt = null;
          
          
           conn = ds.getConnection();
          
           if (conn != null)
           System.out.println("It's OK");
           else
           System.out.println("Tooo bad !");
          
           } catch (Exception e) {
           throw new ServletException(e.getMessage());
           }
           }
          
          



          I'm receiving the same error.

          I wonder which is the right way to use a DS from a servlet...


          Thanks in advance.

          J