insert into db in ejbCreate() ?

Discussions

EJB programming & troubleshooting: insert into db in ejbCreate() ?

  1. insert into db in ejbCreate() ? (4 messages)

    Hi all,

    in ejbCreate(), should I ...
    1) create a record in db without checking duplicate key, or
    2) check for duplicate record, throw DuplicateKeyException if it is the case, otherwise, create a new record, or
    3) do nothing.

    I am being confused by JRun samples...
    it load a record from the db if one is already there, or create one when none exists in its ejbPostCreate()

    Thanks a lot!!!

    Threaded Messages (4)

  2. insert into db in ejbCreate() ?[ Go to top ]

    Hi Andy,
       It's all based on how u r going to design ur real-time system. Please don't get influenced by the code samples of JRun.

    thanks...hasan
    (ahamed at aztec dot soft dot net)
  3. insert into db in ejbCreate() ?[ Go to top ]

    thanks!

    so any rule I can follow ?

    and how to throw a DuplicateKeyException from ejbCreate() is not shown in Richard's book neither, please kindly let me know how ...

    thanks again!
  4. insert into db in ejbCreate() ?[ Go to top ]

    Hi Andy,

    There are 2 situations. one is ur bean is CMP and the other is BMP.

    1. CMP:- u r not writing the insert statement. so u can't catch any SQL exception inside the ejbCreate method.

       ur code will look like this:

           public String ejbCreate(String name, int age
                                   float salary) {
               this.name = name;
               this.age = age;
               this.salary = salary;

               return null;
           }
        
        only on the client side (could be a controller class or a manager session bean) from where u call this method (corresponding create() method of the Home Interface), u've to catch the CreateException and inside the catch block u've to check this instance of CreateException is an instance of DuplicateKeyException. If so, then throw DuplicateKeyException from there. The code will look like this:

          ....
          ....
          try {
              Employee emp = home.create(name, age, salary);
          } catch (CreateException ce) {
              if (ce instanceof DuplicateKeyException) {
                 // either handle it propertly or
                 // do an explicit throw for DKE
                 // to the caller of this method
                 throw (DuplicateKeyException)ce;
              }
          }

    ------------------

    2. BMP:-
        in this case u can easily catch the SQLException and check that this exception has occured due to some duplicate key. (the message contains some error code). You can handle it in the ejbCreateMethod itself or throw an instance of DKE to it's caller.

    thanks...hasan
    (ahamed at aztec dot soft dot net)
  5. insert into db in ejbCreate() ?[ Go to top ]

    hi hasan,

    thank you very much for your detailed explanation and u had helped me a lot!!!

    one question about CMP, how does the container detect DKE ? by examining the SQL results?

    and, can I say that I should not load a record from db in ejbCreate() and JRun gives a wrong example ?

    many many thanks again!!