0 Replies Latest reply on Feb 4, 2003 11:56 PM by Jim Jonah

    Strange Commit Level C behavior

    Jim Jonah Newbie

      Jboss 2.4.10, mySQL Max 3.23.55 both on Linux.

      Using a CMP entity bean, a simple PK class (int for the id field), a simple database table (InnoDB) with only a few fields, one of which is the key field set to autogenerate, I'm running into a random problem.

      I'll call a create method on the entity bean, set all of the fields except the field corresponding to the primary key in the database (and the PK class), since the database will autogenerate the key.

      I'm calling this from a stateless sessionbean which has started a UserTransaction before calling the create on the entity bean. After calling create I'll commit the transaction, exit the method in the sessionbean, return control to a simple java app, and exit the app.

      Randomly when I run the app again to create more records I'll get DUPLICATE KEY errors. In reality things are working correctly on the database side of this.

      When the entity bean is created the PK value is set to 0 by the container. I would have assumed that since I set the commit level to C (standardjboss.xml) that after the transaction ended the container would reload the entity bean and replace the PK value of 0 with the actual value from the database.

      The duplicate key error states that the value of the PK is 0. That means that the first PK is still hanging around in the container.

      I've tried calling a remove() on the entitybean right after the create (after the commit on the transaction), which of course throws a RemoveException (record not found - since the real value of the Key isn't 0). I'd be semi-OK with this if it was consistent and it consistently removed the dangling PK object from the container. It appears that the remove randomly removes the dangling PK.

      So, the question is:

      If level C isn't working (I assumed it would clear the cache after every transaction ends), and myObject.remove() isn't working, is there something I'm missing here?

      Basically, since my key is autogenerated, my PK class is initially set to 0, which doesn't match the real value in the database. I need to either "reset" the PK to the correct value, or remove the PK set to zero from the container consistently.

      I'd really like to do this without switching over the BMP and without remove autogenerate from my primary keys.

      thoughts?

      Jim