4 Replies Latest reply on Mar 17, 2003 10:00 AM by davidjencks

    [u]confusing[/u] problem with jBoss3 and mySQL

    alan_ire


      Hello All,

      I am having a very strane problem with my JDBC connection. I am running jboss3.0.4-tomcat and mySQL3.23. I have set my datasource to mySQL using the mysql-service.xml file and changed the standardJAWS.xml and standardjboss.xml files accordingly. jBoss seems to load up ok but when I run my BMP Bean Client the server throws an error. The error is this:

      07:15:46,783 INFO [MainDeployer] Starting deployment of package: file:/C:/jboss/server/default/deploy/Product.jar
      07:15:46,833 INFO [EjbModule] Creating
      07:15:46,853 INFO [EjbModule] Deploying BMPProduct
      07:15:46,883 INFO [EjbModule] Created
      07:15:46,883 INFO [EjbModule] Starting
      07:15:46,943 INFO [EjbModule] Started
      07:15:46,943 INFO [MainDeployer] Deployed package: file:/C:/jboss/server/default/deploy/Product.jar
      07:15:55,095 INFO [STDOUT] setEntityContext
      07:15:55,105 INFO [STDOUT] ejbCreate
      07:15:55,105 INFO [STDOUT] <-- got this far 1
      07:15:55,105 INFO [STDOUT] <-- got this far A
      07:15:55,105 INFO [STDOUT] <-- got this far B
      07:15:55,105 INFO [STDOUT] <-- got this far C
      07:15:55,105 INFO [STDOUT] connection = DriverManager.getConnection(jdbc:mysql://localhost:3306/MyDB , null, null)
      ;
      07:15:55,115 INFO [STDOUT] java.sql.SQLException: No suitable driver
      07:15:55,115 INFO [STDOUT] <-- got this far 2
      07:15:55,115 ERROR [LogInterceptor] EJBException, causedBy:
      java.lang.NullPointerException
      at com.brainysoftware.ejb.ProductBean.ejbCreate(ProductBean.java:62)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      ...


      The error on the client side is:

      Got context
      Got reference
      java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
      java.rmi.ServerException: EJBException:; nested exception is:
      javax.ejb.EJBException: null; CausedByException is:
      null

      The file that the error seems to be coming from is:
      package com.brainysoftware.ejb;

      import java.sql.*;
      import java.util.Properties;
      import java.util.Enumeration;
      import java.util.Vector;
      import java.rmi.RemoteException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;
      import javax.ejb.CreateException;
      import javax.ejb.FinderException;
      import javax.ejb.ObjectNotFoundException;
      import javax.naming.InitialContext;
      import javax.naming.Context;
      import javax.naming.NamingException;

      public class ProductBean implements EntityBean {

      EntityContext context;
      int productId;
      String productName;
      String description;
      double price;

      public int getProductId() {
      System.out.println("getProductId");
      return productId;
      }

      public String getProductName() {
      System.out.println("getProductName");
      return productName;
      }

      public String getDescription() {
      System.out.println("getDescription");
      return description;
      }
      public double getPrice() {
      System.out.println("getPrice");
      return price;
      }

      public ProductPK ejbCreate(int productId, String productName,
      String description, double price)
      throws RemoteException, CreateException {
      System.out.println("ejbCreate");
      this.productId = productId;
      this.productName = productName;
      this.description = description;
      this.price = price;
      Connection con = null;
      PreparedStatement ps = null;
      try {
      String sql = "INSERT INTO Products" +
      " (ProductId, ProductName, Description, Price)" +
      " VALUES" +
      " (?, ?, ?, ?)";
      System.out.println("<-- got this far 1");
      con = getConnection();
      System.out.println("<-- got this far 2");
      ps = con.prepareStatement(sql);
      System.out.println("<-- got this far 3");
      ps.setInt(1, productId);
      ps.setString(2, productName);
      ps.setString(3, description);
      ps.setDouble(4, price);
      ps.executeUpdate();
      System.out.println("<-- got this far 4");
      }
      catch (SQLException e) {
      System.out.println(e.toString());
      }
      finally {
      try {
      if (ps!=null)
      ps.close();
      if (con!=null)
      con.close();
      }
      catch (SQLException e) {
      }
      }
      return new ProductPK(Integer.toString(productId));
      }


      public void ejbPostCreate(int productId, String productName,
      String description, double price)
      throws RemoteException, CreateException {
      System.out.println("ejbPostCreate");
      }

      public ProductPK ejbFindByPrimaryKey(ProductPK primaryKey)
      throws RemoteException, FinderException {
      System.out.println("ejbFindByPrimaryKey");
      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
      String sql = "SELECT ProductName" +
      " FROM Products" +
      " WHERE ProductId=?";
      int productId = Integer.parseInt(primaryKey.productId);
      con = getConnection();
      ps = con.prepareStatement(sql);
      ps.setInt(1, productId);
      rs = ps.executeQuery();
      if (rs.next()) {
      rs.close();
      ps.close();
      con.close();
      return primaryKey;
      }
      }
      catch (SQLException e) {
      System.out.println(e.toString());
      }
      finally {
      try {
      if (rs!=null)
      rs.close();
      if (ps!=null)
      ps.close();
      if (con!=null)
      con.close();
      }
      catch (SQLException e) {
      }
      }
      throw new ObjectNotFoundException();

      }

      public Enumeration ejbFindByName(String name)
      throws RemoteException, FinderException {
      System.out.println("ejbFindByName");
      Vector products = new Vector();
      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;

      try {
      String sql = "SELECT ProductId " +
      " FROM Products" +
      " WHERE ProductName=?";
      con = getConnection();
      ps = con.prepareStatement(sql);
      ps.setString(1, name);
      rs = ps.executeQuery();
      while (rs.next()) {
      int productId = rs.getInt(1);
      products.addElement(new ProductPK(Integer.toString(productId)));
      }
      }
      catch (SQLException e) {
      System.out.println(e.toString());
      }
      finally {
      try {
      if (rs!=null)
      rs.close();
      if (ps!=null)
      ps.close();
      if (con!=null)
      con.close();
      }
      catch (SQLException e) {
      }
      }
      return products.elements();
      }

      public void ejbRemove() throws RemoteException {
      System.out.println("ejbRemove");
      Connection con = null;
      PreparedStatement ps = null;
      try {
      String sql = "DELETE FROM Products" +
      " WHERE ProductId=?";
      ProductPK key = (ProductPK) context.getPrimaryKey();
      int productId = Integer.parseInt(key.productId);
      con = getConnection();
      ps = con.prepareStatement(sql);
      ps.setInt(1, productId);
      ps.executeUpdate();
      }
      catch (SQLException e) {
      System.out.println(e.toString());
      }
      finally {
      try {
      if (ps!=null)
      ps.close();
      if (con!=null)
      con.close();
      }
      catch (SQLException e) {
      }
      }
      }

      public void ejbActivate() {
      System.out.println("ejbActivate");
      }

      public void ejbPassivate() {
      System.out.println("ejbPassivate");
      }

      public void ejbLoad() {
      System.out.println("ejbLoad");
      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
      String sql = "SELECT ProductName, Description, Price" +
      " FROM Products" +
      " WHERE ProductId=?";
      con = getConnection();
      ps = con.prepareStatement(sql);
      ps.setInt(1, this.productId);
      rs = ps.executeQuery();
      if (rs.next()) {
      this.productName = rs.getString(1);
      this.description = rs.getString(2);
      this.price = rs.getDouble(3);
      }
      }
      catch (SQLException e) {
      System.out.println(e.toString());
      }
      finally {
      try {
      if (rs!=null)
      rs.close();
      if (ps!=null)
      ps.close();
      if (con!=null)
      con.close();
      }
      catch (SQLException e) {
      }
      }
      }
      public void ejbStore() {
      System.out.println("ejbStore");
      Connection con = null;
      PreparedStatement ps = null;
      try {
      String sql = "UPDATE Products" +
      " SET ProductName=?, Description=?, Price=?" +
      " WHERE ProductId=?";
      ProductPK key = (ProductPK) context.getPrimaryKey();
      int productId = Integer.parseInt(key.productId);
      con = getConnection();
      ps = con.prepareStatement(sql);
      ps.setString(1, this.productName);
      ps.setString(2, this.description);
      ps.setDouble(3, this.price);
      ps.setInt(4, productId);
      ps.executeUpdate();
      }
      catch (SQLException e) {
      System.out.println(e.toString());
      }
      finally {
      try {
      if (ps!=null)
      ps.close();
      if (con!=null)
      con.close();
      }
      catch (SQLException e) {
      }
      }


      }
      public void setEntityContext(EntityContext context) {
      System.out.println("setEntityContext");
      this.context = context;

      }

      public void unsetEntityContext() {
      System.out.println("unsetEntityContext");
      context = null;
      }

      public Connection getConnection() {

      String dbUrl = null;
      String userName = null;
      String password = null;
      Context initialContext;
      Context environment;
      Connection connection = null;

      try {
      initialContext = new InitialContext();
      environment = (Context) initialContext.lookup("java:comp/env");
      System.out.println("<-- got this far A");
      dbUrl = "jdbc:mysql://localhost:3306/MyDB"; //(String) environment.lookup("dbUrl");
      System.out.println("<-- got this far B");
      // userName = (String) environment.lookup("dbUserName");
      // password = (String) environment.lookup("dbPassword");
      }
      catch (NamingException e) {
      System.out.println(e.toString());
      }
      try {
      System.out.println("<-- got this far C");
      System.out.println("connection = DriverManager.getConnection(" + dbUrl + " , " + userName + ", " + password + ");");
      connection = DriverManager.getConnection(dbUrl, userName, password);
      System.out.println("<-- got this far D");
      }
      catch (SQLException e) {
      System.out.println(e.toString());
      }
      return connection;
      }
      }




      I have checked the JDBC connector by writing an other java program and there is no problem. I have also tried different JDBC drivers. I have also tried every related topic on this forum and on all the others. <-- this is my last port of call.

      The really funny thing about this error is that I ran the same code on jBoss 2.4 to see if it would work and it did.

      please if you know anything that may help let me know.

      many thanks in advance.

      alan.

        • 1. Re: [u]confusing[/u] problem with jBoss3 and mySQL
          davidjencks

          I advise you to use the jboss pooling.

          Probably you have not deployed the mysql driver properly in a place jboss can find it.

          • 2. Re: [u]confusing[/u] problem with jBoss3 and mySQL
            alan_ire

            Hi David,

            Thanks for the reply. I'm sure the drivers in the right place. I have it in the %JBOSS%\server\deploy\lib. I also have it in the JBOSS_CLASSPATH and in the %JAVA_HOME%\jre\lib\ext directories.

            I tried it again with all the different drivers and gotten the same error message. I am using JDK1.4 so I tried JDK1.3 but again it made no difference to the error.

            I thought I was using Pooling? Any more suggestions would be more than welcome.

            Thanks Again,
            Alan.

            • 3. Re: [u]confusing[/u] problem with jBoss3 and mySQL
              alan_ire

              Hi All,

              I have managed to fix the problem. I'm not exactly sure what the problem was. I think the datasource was configured correctly but when I deployed my jar file the config xml files JNDI names where pointing to another database that I didn't have a driver for. After re-installing jBoss again, again, again and again and again the problems ironed themselves out.

              Cheers,
              Alan.

              "Happy St.Patricks Day"

              • 4. Re: [u]confusing[/u] problem with jBoss3 and mySQL
                davidjencks

                Using DriverManager means you are not using pooling. Look up in jndi the datasource you deployed and use it if you wish to use pooling.

                Only put the driver classes in ONE place, preferably .../server/[config-name]/lib. having them in more than one place is obviously unnecessary, and having them on any startup classpath is apt to lead to mysterious problems.