3 Replies Latest reply on Dec 18, 2003 4:37 AM by milowe

    ejbLoad error

    kargenc

      hi all,

      my all ejb codes running successfully in BEA,SUNONE,and Generic Application server.

      i have this error in JBoss

      When i call ejbFindByPrimaryKey and setting (exm. this.userid = userid) and sql statement is run successfully. When ejb automatically call after this method ejbLoad() this.userid is null
      my code is

      *******************

      public class loginBean implements EntityBean {

      protected EntityContext ctx;

      public String userid;
      public String username;
      public String usersurname;
      public String useremail;
      public int isadmin;



      public loginBean() {
      }

      public void setUserid(String userid) {
      this.userid = userid;
      }

      public void setUsername(String username) {
      this.username = username;
      }

      public void setUsersurname(String usersurname) {
      this.usersurname = usersurname;
      }

      public void setUseremail(String useremail) {
      this.useremail = useremail;
      }


      public void setIsadmin(int isadmin) {
      this.isadmin = isadmin;
      };
      public String getUserid() {
      return this.userid;
      }

      public String getUsername() {
      return this.username;

      }

      public String getUsersurname() {
      return this.usersurname;
      }

      public String getUseremail() {
      return this.useremail;
      };

      public int getIsadmin() {
      return this.isadmin;
      }

      /** Creates a new instance of loginBean */

      public void ejbActivate() {
      }

      public void ejbRemove() throws RemoveException {

      java.sql.PreparedStatement stmt = null;
      java.sql.Connection con = null;

      try {
      con = getConnection();
      stmt = con.prepareStatement("delete from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);

      if (stmt.executeUpdate() == 0 ) {
      System.out.println("Cant remove user");
      throw new RemoveException(" User " + this.userid + " failed to be removed from database");
      }
      }
      catch (Exception ex) {
      System.out.println(ex.toString());
      throw new EJBException(ex.toString());
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("delete from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);

      if (stmt.executeUpdate() == 0 ) {
      System.out.println("Cant remove user");
      throw new RemoveException(" User " + this.userid + " failed to be removed from database");
      }
      }
      catch (Exception ex) {
      System.out.println(ex.toString());
      throw new EJBException(ex.toString());
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("delete from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);

      if (stmt.executeUpdate() == 0 ) {
      System.out.println("Cant remove user");
      throw new RemoveException(" User " + this.userid + " failed to be removed from database");
      }
      }
      catch (Exception ex) {
      System.out.println(ex.toString());
      throw new EJBException(ex.toString());
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("delete from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);

      if (stmt.executeUpdate() == 0 ) {
      System.out.println("Cant remove user");
      throw new RemoveException(" User " + this.userid + " failed to be removed from database");
      }
      }
      catch (Exception ex) {
      System.out.println(ex.toString());
      throw new EJBException(ex.toString());
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }


      }

      public void ejbPassivate() {
      }

      public void ejbLoad() {

      java.sql.PreparedStatement stmt = null;
      java.sql.Connection con = null;

      try {
      con = getConnection();
      stmt = con.prepareStatement("select username, usersurname, useremail, isadmin from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);


      java.sql.ResultSet rs = stmt.executeQuery();

      rs.next();

      this.username = rs.getString("username");
      this.usersurname = rs.getString("usersurname");
      this.useremail = rs.getString("useremail");
      this.isadmin = rs.getInt("isadmin");
      }
      catch (Exception ex) {
      System.out.println("User " + this.userid + " failed load from database");
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("select username, usersurname, useremail, isadmin from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);


      java.sql.ResultSet rs = stmt.executeQuery();

      rs.next();

      this.username = rs.getString("username");
      this.usersurname = rs.getString("usersurname");
      this.useremail = rs.getString("useremail");
      this.isadmin = rs.getInt("isadmin");
      }
      catch (Exception ex) {
      System.out.println("User " + this.userid + " failed load from database");
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("select username, usersurname, useremail, isadmin from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);


      java.sql.ResultSet rs = stmt.executeQuery();

      rs.next();

      this.username = rs.getString("username");
      this.usersurname = rs.getString("usersurname");
      this.useremail = rs.getString("useremail");
      this.isadmin = rs.getInt("isadmin");
      }
      catch (Exception ex) {
      System.out.println("User " + this.userid + " failed load from database");
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("select username, usersurname, useremail, isadmin from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);


      java.sql.ResultSet rs = stmt.executeQuery();

      rs.next();

      this.username = rs.getString("username");
      this.usersurname = rs.getString("usersurname");
      this.useremail = rs.getString("useremail");
      this.isadmin = rs.getInt("isadmin");
      }
      catch (Exception ex) {
      System.out.println("User " + this.userid + " failed load from database");
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }


      }

      public void ejbStore() {
      java.sql.PreparedStatement stmt = null;
      java.sql.Connection con = null;

      try {
      con = getConnection();
      stmt = con.prepareStatement("update dbo.usrUser set username = ? , usersurname = ? , useremail = ? where userid = ?");
      stmt.setString(1, this.username);
      stmt.setString(2,this.usersurname);
      stmt.setString(3,this.useremail);
      stmt.setString(4,this.userid);

      stmt.executeUpdate();

      }


      catch (SQLException ex) {
      System.out.println("User " + this.userid + " failed save to database with this message " + ex.getSQLState());
      } catch(Exception e) {
      System.out.println(e.getMessage());
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("update dbo.usrUser set username = ? , usersurname = ? , useremail = ? where userid = ?");
      stmt.setString(1, this.username);
      stmt.setString(2,this.usersurname);
      stmt.setString(3,this.useremail);
      stmt.setString(4,this.userid);

      stmt.executeUpdate();

      }


      catch (SQLException ex) {
      System.out.println("User " + this.userid + " failed save to database with this message " + ex.getSQLState());
      } catch(Exception e) {
      System.out.println(e.getMessage());
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("update dbo.usrUser set username = ? , usersurname = ? , useremail = ? where userid = ?");
      stmt.setString(1, this.username);
      stmt.setString(2,this.usersurname);
      stmt.setString(3,this.useremail);
      stmt.setString(4,this.userid);

      stmt.executeUpdate();

      }


      catch (SQLException ex) {
      System.out.println("User " + this.userid + " failed save to database with this message " + ex.getSQLState());
      } catch(Exception e) {
      System.out.println(e.getMessage());
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      try {
      con = getConnection();
      stmt = con.prepareStatement("update dbo.usrUser set username = ? , usersurname = ? , useremail = ? where userid = ?");
      stmt.setString(1, this.username);
      stmt.setString(2,this.usersurname);
      stmt.setString(3,this.useremail);
      stmt.setString(4,this.userid);

      stmt.executeUpdate();

      }


      catch (SQLException ex) {
      System.out.println("User " + this.userid + " failed save to database with this message " + ex.getSQLState());
      } catch(Exception e) {
      System.out.println(e.getMessage());
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }



      }

      public void setEntityContext(EntityContext ctx) {
      this.ctx = ctx;
      }


      public void unsetEntityContext() {
      this.ctx = null;
      }

      public void ejbPostCreate(String userid, String username, String usersurname, String useremail, int isadmin) {
      }


      public String ejbCreate(String userid, String username, String usersurname, String useremail, int isadmin) throws CreateException {
      java.sql.PreparedStatement stmt = null;
      java.sql.Connection con = null;

      try {
      this.useremail = useremail;
      this.userid = userid;
      this.username = username;
      this.usersurname = usersurname;
      this.isadmin = isadmin;

      con = getConnection();
      stmt = con.prepareStatement("insert into dbo.usrUser(userid,username,usersurname,useremail,isadmin) values(?,?,?,?,?)");

      stmt.setString(1, this.userid);
      stmt.setString(2, this.username);
      stmt.setString(3, this.usersurname);
      stmt.setString(4, this.useremail);
      stmt.setInt(5, this.isadmin);
      stmt.executeUpdate();
      }
      catch (Exception ex) {
      System.out.println("User " + this.userid + " failed save to database");
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      setUserid(userid);
      setUsername(username);
      setUsersurname(usersurname);
      setUseremail(useremail);
      setIsadmin(isadmin);
      return this.userid;
      }
      /** Creates a new instance of entgroupbean */


      public String ejbFindByPrimaryKey(String userid) throws FinderException {
      java.sql.PreparedStatement stmt = null;
      java.sql.Connection con = null;
      this.userid = userid;

      try {
      con = getConnection();
      stmt = con.prepareStatement("select userid from dbo.usrUser where userid = ?");
      stmt.setString(1, this.userid);

      java.sql.ResultSet rs = stmt.executeQuery();

      rs.next();

      }
      catch (Exception ex) {
      System.out.println("User " + this.userid + " failed load from database");
      }

      finally {
      try { if (stmt!=null) stmt.close();} catch (Exception e) {};
      try { if (con!=null) con.close();} catch (Exception e) {};
      }
      return this.userid;

      }




      public java.sql.Connection getConnection() throws Exception {

      InitialContext initCtx = new InitialContext();

      javax.sql.DataSource ds = (javax.sql.DataSource)initCtx.lookup("poolbugtrace");
      return ds.getConnection();

      }

      *****

      Commit Method is B
      this is a BMP 2.0
      Datasource is MSSQL 2000 driver is with ServicePack1 and CursorMode is cursor
      and succesfully work other application server

        • 1. Re: ejbLoad error

          It should not be using this.anything in those places.
          The correct place to get the primary key is
          entityContext.getPrimaryKey()

          You definitely shouldn't touch "this" for the finder, it is a home invocation
          which has no primary key.

          Regards,
          Adrian

          • 2. Re: ejbLoad error
            kargenc

            i was solve. When i install 3.2.3 this problem is fixed. i am not change any code. may this problem in 3.1.2. thanks all

            • 3. Re: ejbLoad error
              milowe

              The code is wrong as Adrian already stated. The ejbFindByPrimaryKey should return the primary key for the object to load and not set any instance variables. The object instance from the pool for you to use will not be available until its context is set and its loaded.