Problem in EntityBean

Discussions

EJB design: Problem in EntityBean

  1. Problem in EntityBean (1 messages)

    Hello All, i have develop a simple Entity Bean (BMP), from Mastring_EJB2 by theserverside....

    it is the very first example of entity bean what i am getting wrong when i change the client side code and use only findByPrimaryKey method to get a refference of remote interface...

    Account home=null;

    account=home.findByPrimaryKey(new AccountPK("1234-456-789"));
    account.getBalance(); //it will give the right output that is 100.
    now when i call the deposit method on it like
    account.deposit(500);
    it will give me the right output i-e 600 but record in the DB is not updated, when i see the output which is debugged on server side the ejbStore() gives the followin output

    22:59:36,351 INFO [STDOUT] ejbStore() Called
    22:59:36,351 INFO [STDOUT] Raheel 100.0 null

    the third value that is null is the primary key, i don't no why it is shows null because i have the refference of remote interface when i call the ejbFindByPrimaryKey and when i query to get the OwnerName() after getting the refference it will give me a accurate result and also when deposit() method called it did the right thing by adding 500 but it add only in the entity bean instance not updated in the database....


    code of ejbLoad() is as following

    public void ejbStore() {
    System.out.println("ejbStore() called.");
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
    /*
     * 1) Acquire a new DB Connection
     */
    conn = getConnection();
    /*
     * 2) Store account in DB
     */
    pstmt = conn.prepareStatement("update accounts set ownerName = ?, balance = ? where id = ?");
    pstmt.setString(1, ownerName);
    pstmt.setDouble(2, balance);
    pstmt.setString(3, accountID);
    pstmt.executeUpdate();
    System.out.println(ownerName+" "+balance+" "+accountID);
    }
    catch (Exception ex) {
    throw new EJBException("Account " + accountID + " failed to save to database", ex);
    }
    finally {
    /*
     * 3) Release the DB Connection
     */
    try { if (pstmt != null) pstmt.close(); }
    catch (Exception e) {}
    try { if (conn != null) conn.close(); }
    catch (Exception e) {}
    }
    }

    code of ejbFindByPrimaryKey is as following...


    public AccountPK ejbFindByPrimaryKey(AccountPK key) throws FinderException {
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
    System.out.println("ejbFindByPrimaryKey(" + key + ") called");
    /*
     * Acquire DB connection
     */
    conn = getConnection();
    /*
     * Find the Entity in the DB
     */
    pstmt = conn.prepareStatement("select id from accounts where id = ?");
    pstmt.setString(1, key.toString());
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    /*
     * No errors occurred, so return the Primary Key
     */
    return key;
    }
    catch (Exception e) {
    throw new FinderException(e.toString());
    }
    finally {
    /*
     * Release DB Connection for other beans
     */
    try { if (pstmt != null) pstmt.close(); }
    catch (Exception e) {}
    try { if (conn != null) conn.close(); }
    catch (Exception e) {}
    }
    }


    i am really stuck cause of it, any help from you friends will be great for me.
    with best regards.. and thanks in advacne...
    Raheel
  2. Problem in EntityBean[ Go to top ]

    A classic beginner mistake with BMP entity beans is to retrieve primary keys values from instance variables. Given the way EJB pooling works, these instance variable may hold meaningless values. Try using the EntityContext.getPrimaryKey() method instead (which will return your AccountPK).

    To initialize the EntityContext:


    private EntityContext entityContext;

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


    Try this in ejbStore():


    public void ejbStore() {
      System.out.println("ejbStore() called.");
      PreparedStatement pstmt = null;
      Connection conn = null;
      try {
        /*
         * 1) Acquire a new DB Connection
         */
        conn = getConnection();
        /*
         * 2) Store account in DB
         */
        pstmt = conn.prepareStatement("update accounts set ownerName = ?, balance = ? where id = ?");
        pstmt.setString(1, ownerName);
        pstmt.setDouble(2, balance);
        /********** Begin updated lines ********************* /
        pstmt.setString(3, entityContext.getPrimaryKey().toString());
        pstmt.executeUpdate();
        System.out.println(ownerName+" "+balance+" "+entityContext.getPrimaryKey().toString());
        /********** End updated lines ********************* /
      }
      catch (Exception ex) {
        throw new EJBException("Account " + accountID + " failed to save to database", ex);
      }
      finally {
      /*
       * 3) Release the DB Connection
       */
        try { if (pstmt != null) pstmt.close(); }
        catch (Exception e) {}
        try { if (conn != null) conn.close(); }
        catch (Exception e) {}
      }
    }


    Check your other EJB lifecycle methods for similar errors: ejbLoad(), ejbRemove(). In fact, the problem is probably in ejbLoad() rather than ejbStore().