ejbCreate() and autoincrements

Discussions

EJB programming & troubleshooting: ejbCreate() and autoincrements

  1. ejbCreate() and autoincrements (7 messages)

    Hi,

    Does anyone have a good suggestion of how to handle autoincrementing primary keys in cmp entity beans.

    When the container calls ejbCreate(...) and the creates the database row, does both of these (ejbCreate and the creation of the database row) run within the same transaction?

    I am thinking of doing it like this:

    public Long ejbCreate()
    {
        // get the current maximum pk value
        // increment value
        // return incremented value as the pk of this entity bean
    }

    This method will only work if the container calls ejbCreate(...) and creates the database row within the same transaction.

    I am also thinking of rather than doing a straight jdbc call to the database to get the current maximum pk value, I do it by getting a reference to the entity bean corresponding to that maximum pk value.

    Does anyone have any ideas on this?

    Threaded Messages (7)

  2. ejbCreate() and autoincrements[ Go to top ]

    Have a look in the patterns section, there are a few patterns that deal with primary-key generation. The 'High-Low' pattern is one example.

    The general idea is that you have a cache of primary keys, rather than a database call.

    Cheers,
    Rick
  3. ejbCreate() and autoincrements[ Go to top ]

    Thanks Rick for your pointers. I have read the sections in the patterns section and the High-Low algorithm on unique object ids.

    Only problem is that I am working on top of an existing database and the primary key field is a autoincrement integer.

    I need the entries entered to be in incrementing order because some other application will also be accessing the database. Therefore caching will not really work for me.


  4. ejbCreate() and autoincrements[ Go to top ]

    If it is an autoincrement integer in the database, then you probably needn't specify it in the ejbCreate, the database automatically fills out the field with the creation of a new record. You only need to get it to know which record you are adressing.

    Jeroen
  5. ejbCreate() and autoincrements[ Go to top ]

    But in CMP, the bean needs to be initialised with the primary key prior to insertion.

    I have decided that the only way to get around autoincrements is to use BMP. Correct me if i am wrong....
  6. ejbCreate() and autoincrements[ Go to top ]

    Yes, or use a construction in your create in which you fetch the primary key from the database. It isn't illegal to use SQL or database accesses in a CMP bean ;)
    But I agree it defies the purpose. What I am currently doing myself is writing a wrapper session bean which initializes the primary key value (Still working on the generation algorithm though), and in this way I get all the advantages of a CMP bean without knowing on the outside about primary keys.

    Jeroen
  7. ejbCreate() and autoincrements[ Go to top ]

    Yeah, I thought about the session bean wrapper as well. I quite like that approach and would go for it but I have decided to go with BMP coz I have a new requirement, i.e. read-only entity beans.

    Sorry to veer of the topic but do you know the answer to the following:

    For a BMP, if I am currently in a transaction and I am using short term connections, i.e within the ejbStore() and other database access methods I get the java.sql.Connection from a DataSource and then disconnect (call con.close()) at the end of these methods, does con.close() automatically commit the transaction?

  8. ejbCreate() and autoincrements[ Go to top ]

    It should call in a commit. Isn't it in the API documentation? You would be probably safest to call commit yourself, then you needn't worry ;)

    Jeroen