Version 4

    Optimizing create operation

     

    When an application invokes a create method on the home interface the container does the following:

    1. invokes the corresponding ejbCreate method on the instance;

    2. issues 'SELECT COUNT() FROM <A_TABLE> WHERE <PK_COLUMNS>=?' to check for duplicate keys before issueing SQL INSERT and if the result is not 0 the javax.ejb.DuplicateKeyException is thrown;

    3. issues SQL INSERT;

    4. invokes the corresponding ejbPostCreate method on the instance;

    5. if after ejbPostCreate the instance is dirty, i.e. its CMP and/or CMR fields were modified in ejbPostCreate, then at synchronization time (transaction commit, finder or removal) the container will issue SQL UPDATE to update the data in the database.

     

    This process can be optimized:

    1. invoke the corresponding ejbCreate method on the instance;

    2. invoke the corresponding ejbPostCreate method on the instance;

    3. issue SQL INSERT.

     

    The difference is that in the optimized process the container

    1. delays the SQL INSERT until after after ejbPostCreate returns thus eliminating subsequent SLQ UPDATE if the instance was modified in the ejbPostCreate

    2. and does not issue 'SELECT COUNT()' to check for duplicate keys. Instead, the container will try to guess the unique key constraint violation from the SQL error code if the INSERT fails.

     

    The delayed INSERT is configured with insert-after-ejb-post-create.

    To avoid the 'SELECT COUNT()' you need to use no-select-before-insert entity-command which is specified in jbosscmp-jdbc.xml, for example:

          <entity>
             <ejb-name>A</ejb-name>
         <entity-command name="no-select-before-insert"></entity-command>
          </entity>