1 Reply Latest reply on Feb 25, 2005 7:33 AM by kamal_tavant

    EJB & Performance

    fabboco

      Hi,

      I have a very simple EJB deployed to jboss 3.2.6 + postgresql 8 + java 1.4.2_06 + suse linux 9.2

      I use the following code to make a list:

      HashMap[] certificazioneArray = new HashMap[certificazioni.size()];
       Iterator iCertificazioni = certificazioni.iterator();
       int idx=0;
       while(iCertificazioni.hasNext())
       {
       CertificazioneLocal cert = (CertificazioneLocal)iCertificazioni.next();
      
       HashMap certBean = new HashMap();
      
       certBean.put("id",cert.getId());
       certBean.put("chiusa",cert.getChiusa());
       certBean.put("simulazione",cert.getSimulazione());
       certBean.put("numeroRomano",cert.getNumeroRomano());
       certBean.put("data",cert.getData());
      
       certificazioneArray[idx++]=certBean;
       }
      
      


      Problem is that for each get method jBoss fires a query to the database.

      Using the documentation, I have tried to optimize the sql using load groups (from my jbosscmp-jdbc):

       <entity>
       <ejb-name>Certificazione</ejb-name>
      
       <read-ahead>
       <strategy>on-find</strategy>
       <page-size>1000</page-size>
       <eager-load-group>lista</eager-load-group>
       </read-ahead>
      
       <table-name>certificazione</table-name>
      
       <load-groups>
       <load-group>
       <load-group-name>lista</load-group-name>
       <field-name>chiusa</field-name>
       <field-name>simulazione</field-name>
       <field-name>numeroRomano</field-name>
       <field-name>data</field-name>
       </load-group>
       </load-groups>
      
       <query>
       <query-method>
       <method-name>findAll</method-name>
       <method-params></method-params>
       </query-method>
       <jboss-ql>select object(o) from Certificazione as o order by o.numero</jboss-ql>
       </query>
       </entity>
      


      Nothing seams to change and five queries are still fired.

      If I change the <commit-option> in jboss.xml to A, jboss fires only on sql statement to the database but apparently speed speed is much more better.

      The code I am using for making the list is not into a transaction.

      What's wrong ? Where can I find more documentation on these issues ?

      Thank you.

      Fabrizio




      From my ejb-jar:
       <entity>
       <description>Entity Bean ( CMP )</description>
       <display-name>Certificazione</display-name>
       <ejb-name>Certificazione</ejb-name>
       <local-home>my.ejb.CertificazioneLocalHome</local-home>
       <local>my.ejb.CertificazioneLocal</local>
       <ejb-class>my.ejb.CertificazioneBean</ejb-class>
       <persistence-type>Container</persistence-type>
       <prim-key-class>java.lang.Long</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>Certificazione</abstract-schema-name>
       <cmp-field>
       <field-name>id</field-name>
       </cmp-field>
       <cmp-field>
       <field-name>numero</field-name>
       </cmp-field>
       <cmp-field>
       <field-name>data</field-name>
       </cmp-field>
       <cmp-field>
       <field-name>simulazione</field-name>
       </cmp-field>
       <cmp-field>
       <field-name>chiusa</field-name>
       </cmp-field>
       <cmp-field>
       <field-name>numeroRomano</field-name>
       </cmp-field>
       <primkey-field>id</primkey-field>
       <query>
       <query-method>
       <method-name>findAll</method-name>
       <method-params/>
       </query-method>
       <ejb-ql>select object(o) from Certificazione o</ejb-ql>
       </query>
       </entity>
      
      ......
      
       <container-transaction>
       <method>
       <ejb-name>Certificazione</ejb-name>
       <method-name>*</method-name>
       </method>
       <trans-attribute>Required</trans-attribute>
       </container-transaction>
      


        • 1. Re: EJB & Performance
          kamal_tavant

          Where are you running this code.
          If you run this code in the context of an existing transaction it will use only one sql query. If the code is being executed without an existing transaction each get method would lead to a new transaction being started and committed once the get method is completed.

          You should wrap access to your entity beans within a stateless session bean (facade pattern). The stateless session bean methods should be setup with transaction attribute required.

          The updates will cause even serious problems, if you are trying to set two attributes each one will happen in a separate transaction. The first set will go through regardless of what happens to the second one.

          You should use a stateless session bean method to combine these operations and ensure that they happen in a single transaction.

          Also it is a good practice to set the transaction attribute for entity beans to mandatory. This will prevent them from being used outside of a transaction, which can have very serious performance & functional problems