is it Ok to use isModified boolean field?

Discussions

Performance and scalability: is it Ok to use isModified boolean field?

  1. is it Ok to use isModified boolean field? (7 messages)

    Hi, all!

    Is it Ok to use isModified boolean field?
    To make "isModified=true" only in ejbActivate() and after business methods that modifies bean data.

    so ejbLoad() will read data from DB only if isModified==true and ejbStore() will write data only if isModified==true.

    Thanks.

  2. You are right about ejbStore(). However isModified() has nothing to do with ejbLoad(). Calling ejbLoad() at the beginning of a transaction can be prevented if the underlying data is not modified by any other bean or external application. In Weblogic, that is achieved through the db-is-shared in the deployment descriptor.

    Pranab
  3. If i don't use WebLogic?
    And I'm sure that only one bean have acces to all data related to that bean?

    Will that code work properly?
    ----------------------------------------------------------

    import ...

    public class BMP1 implements EntityBean {

      private EntityContext ctx;
      private String accountId;
      private double balance;
      private volatile transient boolean isModified = false;

      public void ejbActivate() {
        isModified = true;
      }

      public void ejbPassivate() {
      }

      public void ejbLoad() {
       if(! isModified) return;
        Connection con = null;
        PreparedStatement ps = null;
        accountId = (String) ctx.getPrimaryKey();
        log("\tejbLoad reads from DB");

        try {
            ...
            } catch (SQLException sqe) {
            ...
        } finally {
          cleanup(con, ps);
        }
        isModified = false;

      }

      public void ejbStore() {
        if (!isModified) return;
        log("\tejbStore writes in DB");

        Connection con = null;
        PreparedStatement ps = null;
        try {
          ...
        } catch(SQLException sqe) {
          ...
        } finally {
          cleanup(con, ps);
        }
        isModified = false;
      }


      public String ejbCreate(String accountId, double initialBalance) throws CreateException {

          isModified = false;
          ...
      }

      public void ejbRemove() {
      }

      public String ejbFindByPrimaryKey(String pk) throws ObjectNotFoundException{

        Connection con = null;
        PreparedStatement ps = null;

        try {
           ...
        } catch (SQLException sqe) {
           ...
        } finally {
          cleanup(con, ps);
        }

        isModified = false;
        return pk;
      }

      public double deposit(double amount) {
        ...
        isModified = true;
        return balance;
      }

      public double withdraw(double amount) throws RemoteException{
        ...
        isModified = true;
        return balance;
      }
    }
  4. Um, no.

    isModified is a member variable of your java class.

    ejbLoad is to load the data from the database. EjbLoad is called to refresh the contents of your java class in case someone else has commited updates to the underlying DB - eg using a command console or another app or another EJB.

    You can never use isModified within your own EJB.

    If you are the only code ever to update the db, then you could use your own flag which is something like 'isLoaded' which you init to false, and then test in ejbLoad, and set to true at the end of ejbLoad. This gives you a bean which reads on initialisation an not other time.

    You probably want to set this to false in passivate just to be safe.

    Jonathan
  5. I meant:

    You can never use isModified within your own ejbLoad().
  6. Hi, Jonathan!

    Let's imagine a situation:
    1. DB can be modified from EJB's only.

    2. I'm SURE that Entity Bean with PK="1" can not affect data that is related to Entity Bean with PK="2".

    If (EJBObject-Entity Bean) link if broken, the ejbPassivate() is called (isModified is set to true). If not, after last ejbStore() (isModified is set to false) no data has been modified in DB. So, the values of local variables in my Entity Bean are synchronized with the DB's ones.

    Why I can't use isModified in ejbLoad()?

    Den.
  7. Sorry...



    "the ejbPassivate() is called (isModified is set to true)" means
    "the ejbActivate() is called (isModified is set to true)"
  8. If the commit option A is chosen (ejb spec p. 118), then there should not be any need to define the isLoaded variable. In that case the container can assume that it has exclusive access to the database and ejbLoad() is not supposed to be called by the container at the beginning of each transaction for a cached entity bean.

    Pranab