Announcement Announcement Module
Collapse
No announcement yet.
Optimistic locking - retry Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Optimistic locking - retry

    I'm using JdbcCursorItemReader for reading business objects and MyBatis with custom optimistic locking on version for updating (the objects can be modified by other APP). The idea is to retry update operation when it fail due to concurrent access exception.
    The solution I was thinking of looks like this:

    Code:
           <step id="update" >
                <tasklet>
                    <chunk reader="reader" processor="processor" writer="writer" retry-limit="5">
                        <retryable-exception-classes>
                            <include class="OptimisticLockingFailureException" />
                        </retryable-exception-classes>
                        <listeners>
                            <listener ref="lockListener"/>
                        </listeners>
                    </chunk>
                </tasklet>
            </step>
    where lockListener would refresh data from DB with for the failed chunk:

    Code:
    public class OptimisticLockWriteListener implements ItemWriteListener<Item> {
    
        @Autowired
        private ItemDao dao;
    
        @Override
        public void onWriteError(Exception exception, List<? extends Item> items) {
            if (exception instanceof OptimisticLockingFailureException) {
                for (Item item : items) {
                    Item newItem = dao.getItem(item.getId());
                    items.remove(item);
                    items.add(newItem);
                }
            }
        }
    }
    Is there any better solution to this problem?

  • #2
    Just defining <retryable-exception-classes> should work. Also specify fully qualified name of the exception to be retried.
    In my opinion, you don't need the OptimisticLockWriteListener for exception retry. You can use the listener to log to file, etc. in case of an exception. Although I am not very sure if the listener will be called in case of a retryable exception.

    Comment


    • #3
      The problem is that I need not only retry but also re-read items that failed. OptimisticLockWriteListener is called but collection is unmodifiable, so the solution does not work anyway. I think I've found a solution here:

      http://code.google.com/p/springbatch...let.java?r=233

      but I think I would need to "tweak" my job definition somehow to make it working.
      Last edited by howieB; Aug 22nd, 2011, 09:05 AM.

      Comment

      Working...
      X