Announcement Announcement Module
Collapse
No announcement yet.
Data become dirty issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data become dirty issue

    Hi there,

    I have a dirty data problem for my application. My application is using Spring/Hibernate. There are POJO , DAO, Service layers. For each method in a service class, I have using several DAOs to achieve business logic. While I test my application, I found a concurrent problem. If I try to create an order for items I own from browser A and browser B at the same time as the same user, both order are created. But Ideally only one order should be created since I will load the items with associated orderId=0 first, then I will update the items with associated orderId = orderId just created. The problem is that is two orders will load the same items with orderId=0 since at that moment, I have not updated orderId for items yet from one of creation of the order. So Second order with loaded the items thinks the items are good and is created. How can I solve this problem?

    I am using HibernateDAOSupport for DAOs. In service classes, there are many DAOs to do business logic in one method. For example, to create an order,

    (1) create order first, save it
    (2) load the items I own(orderId=0 items)
    if items I own are there
    (3) assign created orderId to items
    return create order successful
    else
    delete the created order ( any rollback way from Spring specially in service class)
    return create order fail

    Any help is very much appreciated.

  • #2
    Any hint for this case or for concurrent situation from spring/hibernate?

    Thanks very much.

    Comment


    • #3
      What about give a look at on appfuse project?
      There you will see the use of several design patterns and how to structure your spring project.

      Gilberto

      Comment


      • #4
        Hi Gilberto,

        Thanks for reply. My application is similar structured as appfuse except more complex. In my service classes, there are many DAOs to serve up each method in the classes. This would generate a question about rollback in the service. For example, take a look at method insertOrder() of PetStoreImpl.java in sample of jpetstore from springframework. What would happpen if second dao's call fails? Is there a way to rollback the first call this.orderDao.insertOrder(order) except catch DataAccessException and then delete the inserted order there in the catch section? I am not sure whether Spring can handle this situation or not. So the question is how we can achieve atomic transaction in service layer if we use Spring?

        For my original question, I partially solve the problem by using optimistic locking from Hibernate.

        Comment


        • #5
          I'm new to spring too!
          But the little bit that I know is that if you instruct spring to demarcate your business methods with transaction, it will do it. So, in my application I put that one in the service layer. For example:
          Code:
          Class OrderManager ...{
          private OrderDAO orderDAO;
          private ItemDAO itemDAO;
          
          setts ...
          
          public processOrder(Order order){
                  Iterator i = order.getOrderItems().iterator();
                  while (i.hasNext()) {
                      OrderItem orderItem = (OrderItem) i.next();
                      Item item = orderItem.getItem();
                      item.increase(movimentoEstoqueItem.getQuantidade());
                      itemDAO.saveItem(item);
                  }
                  order.setFlProcessed(new Boolean(true));
                  saveOrder(order);
              }
          }
          So, here we have two actions - itemDAO.save(item) and saveOrder(order) - that have to be in a transaction. Thus if you config spring to demarcate this method, it will put the begin transaction and commit on it or otherwise rollback it if any exception ocurrs. You got it?

          Gilberto

          Comment

          Working...
          X