Discussions

EJB programming & troubleshooting: concurrency problem

  1. concurrency problem (3 messages)

    I use weblogic 6.0 sp1 - SQL2000
    The sesionbean is stateless use CMP the entity beans use CMP.

    I have this customer find method findByXxx(customerID),
    whenever I update/Add a record to customer, I have to call this method to get all the records from the table for this customerID for update/delete/Add.

    I have 2 client updating the same customer to add say primaryemail record each. The customer can have only one primary email.

    Client one and two adds primaryEmail to the same customer,
    The application allows to create 2 primary email for the same customer. I need to prevent this.

    What I assumed, when the client one does the findByXxx(customerID), to add the primaryEmail, the second client should be blocked(ejbStore) at this method findByXxx(customerID), Till client one finishes with adding record.

    This however does not seem to happen,
    Is there a way to prevent this.
    Thank you for suggestion

    Seshadri





    Threaded Messages (3)

  2. concurrency problem[ Go to top ]

    Seshadri,

    It's not clear whether you are adding the primary email of the customer in the implementation of the findByCustomerID(customerID) method itself, or the clients call the findByCustomerID(customerID) method and then add the primary email contact to the record. If it's the latter, and assuming that you are doing something like customer.setPrimaryEmailContact(email) and that your Customer is an entity bean, the concurrency will be handled automatically by the container. In either case, you won't see two email contacts for the same customer. In the worst case, the second client (whether it's client1, or client2) will overwrite the first.
    Please give us a detailed description of your problem.

    - Karthik.
  3. concurrency problem[ Go to top ]

    This is a normal scenario in all web applications.Check this article for optimistic locking.
    http://www.javaworld.com/javaworld/jw-07-2001/jw-0713-optimism.html.

    You cannot block the client from findByXXX method, because he will do findByXXX call and will see the info and might take a break before updating the record. So you cannot block the other clients from looking and updating the record. The best way is to check whether the the record has been updated (in your case primary email address), if so prompt the client somebody has already added the primary email address.
    you cannot block the other clients from vieweing and updating the record until a client finishes looking and updating the record (you don't know how long it's gonna take).
    The article talks about different options of handling this sort of scenario.
    I have handled this by checking the time stamp(in all the tables we have lastupdated column).
  4. concurrency problem[ Go to top ]

    Thanks for responding,

       http://www.javaworld.com/javaworld/jw-07-2001/jw-0713-optimism.html.
       I am not able to view this page. I will try...

      I use weblogic 6.0sp1, sql2000 entity beans are generated
    by Jbuilder4 Enterprise, findByCustomer(customer A) is
    created using WLQuery language.

      The table customer has coloumns
      rowid, seq#, customerid, data

      The primary Key is rowid & seq#
      foreign key is customerid
      data stores XML data.

    The customer can have many records with XML data like name, address, phone, many emails each as seperate record, so basically a customer will have more than 4 records..

    The Email XML can be primary or the other, I could have
    many other types of email, but only one primary email record.

    I was expecting the below,
     when I call findByCustomer(customer A), since weblogic uses ejbStore(), all the records for this customer on read would lock, the second client updating the same customer would have to read this customer data first at which point it would be blocked till client 1 finishes adding record
    and when client 2 calls this findByCustomer(customer A) whcich would now return this added record by Client 1 and I could validate at business logic level.

    The findByCustomer does not block the second client.
    The design could be problem from DB point of view but
    why would the findByCustomer for same customer not get blocked for second client???

    Seshadri