3 Replies Latest reply on May 26, 2008 3:25 AM by Swen Barth

    Many-to-one writes non-existing IDs to the DB occasionally

    Swen Barth Newbie


      I have encountered a strange problem.

      I'm using JBoss4.0.4GA with EJB3.

      I have a global ID scheme for all my "Entity" beans, the IDs are generated by a TableGenerator, and are Longs which start with 100.000 and count upwards.

      I have an instance of bean A with a many-to-one relationship to instances of bean B. Recently I got the follwing exception when loading an certain instance of A from the DB:

      javax.persistence.EntityNotFoundException: Unable to find {bean B} with id 87005

      Which is pefectly sane, because there is no bean B with this ID (I checked in the DB). But I also found in the DB that in the row for bean A, in the column where B's Id should be found, there was indeed the value '87005'. That ID can never have existed before, because as I said, they started with 100.000 and count upwards.
      Does anyone have an explanation for that behaviour? Why was this impossible ID ever written to the DB?
      It is not reproducable, I have many rows in the table for bean A where the relationship to B is just fine. This occured for only 6 of about 6000 beans of type A.

        • 1. Re: Many-to-one writes non-existing IDs to the DB occasional
          Swen Barth Newbie

          Ok, I have some additional info. In one case, the correct ID that SHOULD have been there is id1 = 119773. The wrong id is id2 = 87005.
          If I convert those to binary format, I get:

          id1 = 11101001111011101
          id2 = 10101001111011101

          As you can see, the second bit (from the left) flipped.

          I'm beginning to see where the problem may be... something with number conversion between formats?
          My IDs in the Java classes are of type java.lang.Long, the corresponding fields in the DB (PK and FK columns, autogenerated) are of type BIGINT(20).

          • 2. Re: Many-to-one writes non-existing IDs to the DB occasional
            Swen Barth Newbie

            It's strange, but the behaviour I described above is not reproducable. I checked in the DB and found many rows where the correct value (119773) was written.

            I identified six cases where this behaviour occured, and for all of them, there are other rows in the DB where the value was written correctly.
            Strangely, of the six cases I identified, 3 respectively 2 occured with the same ID. Again, in other rows, those very same IDs were written correctly.

            In all cases, the bit #16 changed from 1 to 0.

            Does anybody have an idea what could be the cause for this strange behaviour?

            • 3. Re: Many-to-one writes non-existing IDs to the DB occasional
              Swen Barth Newbie

              This problem went away after I updated to the newest MySQL JDBC version.