0 Replies Latest reply on Oct 22, 2002 9:32 AM by hezekiel

    INSERTING AN ALREADY EXISTING BEAN & PostgreSQL

    hezekiel

      I think that the error seen in the subject of this message has been left unanswered so many times that I try to give one solution/reason for it here.

      I use BMP so this is different in CMP, but if you got the same error in CMP, it's probably a bug in CMP implementation.

      PostgreSQL and BMP:
      *Don't* use the query "SELECT last_value FROM sometable_seq"!
      Why? Because you get what you asked for - a last inserted primary key for the given table. If you have several entity beans creating records in parallel you might run into a situation that two of you beans get the same pk during the persistence phase. Then after the insert has been made both of those beans report the same pk to the container and it'll throw the aforementioned exception.

      *Do* use the query "SELECT nextval('sometable_seq')"!
      Why? Because this query will give you the next available pk for the given table, and what's more important, it never returns the same pk again. So even if your entity beans run parallel inserts into the database, you will never get the same pk and no exceptions because of that.

      The nextval function marks the given pk as used, even if you don't do the actual insert.