Optimizing create operation
When an application invokes a create method on the home interface the container does the following:
invokes the corresponding ejbCreate method on the instance;
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;
issues SQL INSERT;
invokes the corresponding ejbPostCreate method on the instance;
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:
invoke the corresponding ejbCreate method on the instance;
invoke the corresponding ejbPostCreate method on the instance;
issue SQL INSERT.
The difference is that in the optimized process the container
delays the SQL INSERT until after after ejbPostCreate returns thus eliminating subsequent SLQ UPDATE if the instance was modified in the ejbPostCreate
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>