EJB programming & troubleshooting: Purpose of PrimaryKey class.

  1. Purpose of PrimaryKey class. (7 messages)

    Hi, Can some one tell me the purpose of PrimaryKey class. I thought like if we have a PrimaryKEy class then we no need to have a PKConstraint at the database level. but if u dont have a PKConstraint at the DB level then its possible to create duplicate records even though u have a specified PKClass in the deployment discriptor. Now i would like to know the purpose of PKClass ? Doesn't it meant to maintain the uniqueness ? can some one help me out from this...Correct me if assumption is wrong.


    Threaded Messages (7)

  2. Purpose of PrimaryKey class.[ Go to top ]

    In CMP, the container will have it's own persistence framework. The PrimaryKey class is a mechanism by which u identify an entity uniquely with which u may retreive, update, create or delete. This mechnanism just maps the PrimaryKey mechanism at the database level.

    To put it simple, PrimaryKey Class is the mechanism that identifies a row uniquely at the container's persistence framework level where as the PrimaryKey in the db is the same but at the database level. I just tried my best to answer ur question.
  3. Purpose of PrimaryKey class.[ Go to top ]

    Hi Anil,

    The specification clearly mentions that the container *may* throw a DuplicateKeyExceptionwhen an entity with the same key already exists. So a container may choose not to throw DuplicateKeyException if no database constraints are defined.

  4. Purpose of PrimaryKey class.[ Go to top ]

    Hemant's note is correct, but I just wanted to emphasize the point: you can never create two entity beans with the same PK. The container is not required to throw a DuplicateKeyException, but it is required to ensure the duplicate PK never gets written to the DB. If the container didn't throw DuplicateKeyException, it will rollback the transaction before it completes. The container is allowed to delay the throwing of a DuplicateKeyException for perofrmance reasons.

  5. Purpose of PrimaryKey class.[ Go to top ]

    I think the section from the EJB2.0 final specification says it all.

    " DuplicateKeyException

    The DuplicateKeyException is a subclass of CreateException. It may be thrown by the Container to indicate to the client or local client that the entity object cannot be created because an entity object with the same key already exists. The unique key causing the violation may be the primary key, or another key defined in the underlying database.

    Normally, the Container should not mark the transaction for rollback before throwing the exception.

    When the client or local client receives a DuplicateKeyException, the client knows that the entity was not created, and that the transaction has not typically been marked for rollback."

    For performance reasons, a container may choose to skip existence of a duplicate record if no database constraints are defined. In any case, DuplicateKeyException is an application exception and container is not supposed to mark the transaction for rollback.

  6. Purpose of PrimaryKey class.[ Go to top ]

    Hi thanks for the responses. As per the aboe discussion whether to throw or not is left to container/server specific right(throwing DuplicateKeyException) ? can some tell me which server has implemented that. coz in weblogic i'm able to create the duplicate records.
  7. Purpose of PrimaryKey class.[ Go to top ]

    Hemant, I belive you are looking in the wrong place. The section you mentioned describes the meaning of DuplicateKeyException, not the responsibillities of the container provider. As per sub-section 10.5.3, page 176:

    "The entity object created by the ejbCreate<METHOD> method must have a unique primary key. This means that the primary key must be different from the primary keys of all the existing entity objects within the same home. However, it is legal to reuse the primary key of a previously
    removed entity object. The container may, but is not required to, throw the DuplicateKeyException on the Bean Provider&#8217;s attempt to create an entity object with a
    duplicate primary key[15]."

    The comment [15] states that containers using, for instance, optimistic strategies, may rollback the transaction at a later point. I.e, DuplicateKeyException is not required, but the container *is* required to ensure the uniqueness of the PK by some other means.
    That is what I stated in my post above. Do you have a different interpretation of this subsection of the spec?

  8. Purpose of PrimaryKey class.[ Go to top ]

    no doubt about this interpretation...