8 Replies Latest reply on Apr 28, 2004 2:26 AM by Alexey Loubyansky

    CMP-Bean is stored after each access (reads), Why ?

    Frode Halvorsen Newbie

      I have an CMP-bean and a Sateless Session-bean. The CMP-bean includes a findAll()-method ana a findByName()-method in addition to the findByPrimaryKey()-method.

      The CMP-bean has a method getName().

      No matter what finder-method I use, a call to the getName-method triggers a ejbStore-method.

      When I loop through an iterator for the collection returned by the two finders, the ejbStore is triggered after the loop is finished, not after each ejb-access.

      How can I prevent this store ?

        • 1. Re: CMP-Bean is stored after each access (reads), Why ?
          Alexey Loubyansky Master

          This is fixed in 3.2.4.
          Perhaps, you will be suprised that by spec ejbStore must always be called on the instance at commit time of the transaction even if the instance is not dirty.
          Therefore in JBoss-4 a config option call-ejb-store-on-clean is added which is true by default, i.e. spec compliant, i.e. ejbStore is always called at commit time.

          • 2. Re: CMP-Bean is stored after each access (reads), Why ?
            Frode Halvorsen Newbie

            Thanks for the reply. But Why doesn't the ejbStore get called before the loop is finished ?

            And why doesn't it get called if I just call the toString-method on the Bean (only if I use one of my own get-methods) ?

            This really mens that updating a product-database with thousonds of records with relations is really not an EJB-task. Since the ejbs with relations is also stored...

            And just using a findAll-method for reading a large table is a bad idea...

            This fix is JBoss-specific ?? So another EJB-server would do the same horrible database-access ?

            • 3. Re: CMP-Bean is stored after each access (reads), Why ?
              Frode Halvorsen Newbie

              Another thing that really is a bad idea because of this, is to have an update of a changeDate-field in the CMB-bean updated by the ejbStore-method. Which I have, and is why I discovered this strange behavior... My beans got updated every time I accessed them, not only when I changed them....

              What's the point of the 'dirty'-flag in the spec ?

              • 4. Re: CMP-Bean is stored after each access (reads), Why ?
                Alexey Loubyansky Master

                The ejbStore called before the end of the loop because it is triggered by the finByName finder called in the loop. It is also the spec requirement to synchronize before querying the datastore.

                ejbStore is not called if you invoke toString() just because toString() method does not enlist the instance into the transaction.

                I am not sure whether other vendors optimize ejbStore. I guess they are.

                As to updates of date CMP fields, it is also fixed now. You can use

                <check-dirty-after-get>false</check-dirty-after-get>
                

                for a cmp-field of a date type in jbosscmp-jdbc.xml to prevent the update.

                • 5. Re: CMP-Bean is stored after each access (reads), Why ?
                  Frode Halvorsen Newbie

                  My point was that the ejbStore is NOT called before the loop is finished

                  my code :

                  public List finnProdusenter(){
                  ArrayList rv = new ArrayList();
                  try {
                  Iterator i = pHome.findAll().iterator();
                  while (i.hasNext()){
                  ProdusentBeanLocal tmp = (ProdusentBeanLocal)i.next();
                  // System.out.println(tmp.getNavn());
                  rv.add(new GenericPk(tmp.getId(),tmp.getNavn()));
                  }
                  }catch (Exception e) {
                  // TODO: handle exception
                  }
                  return rv;
                  }

                  ...
                  If i printout the name, alle the name-prints before the store takes place.
                  I see this because I have a printout in the ejbStore-methon in the bean.

                  And the 'check-dirty'-parameter is a JBoss-specific thing, and only from 3.2.4 ?

                  • 6. Re: CMP-Bean is stored after each access (reads), Why ?
                    Alexey Loubyansky Master

                    Why do think it should be called in the loop? Is this loop executed in a single transaction? Check the spec for when the synchronization should happen.

                    • 7. Re: CMP-Bean is stored after each access (reads), Why ?
                      Frode Halvorsen Newbie

                      OK.

                      So when I access different beans insde one session-bean , all access is done in one transaction.

                      That's OK.

                      If I access different methods of an session-bean from an client. Is all of those also within the same transaction ?

                      1. If i access them from an jsp I think they are in different transactions
                      2. If I access them from another session-bean, could they be a part of the same transaction the calling session-bean is in...

                      This really clears up.. The transaction is only one huge from the first call in the first session-bean.

                      Then my Q is :

                      How can I inside one method-call (in a session-bean) end one transaction, and start a new one ?

                      I am updating 20.000-40.000 beans from a text-file, all inside one session-bean, and this doesn't work well as long all updates are being locked, pending a commit.

                      • 8. Re: CMP-Bean is stored after each access (reads), Why ?
                        Alexey Loubyansky Master

                        Please, check the EJB spec for transaction attributes, Bean Managed Transactions and UserTransaction.