Results 1 to 3 of 3

Thread: Optimistic locking - retry

  1. #1
    Join Date
    Dec 2009
    Posts
    6

    Default 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. #2
    Join Date
    May 2011
    Location
    New Delhi, India
    Posts
    157

    Default

    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.

  3. #3
    Join Date
    Dec 2009
    Posts
    6

    Default

    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 at 09:05 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •