Announcement Announcement Module
Collapse
No announcement yet.
Force commit in batch update Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Force commit in batch update

    Hi all,

    I need to do a batch insert/update of a few hundreds records in a table. I am using Spring 1.1.1 with Ibatis sql map. What I want to achieve is this:

    (assuming total no of records = 100)
    Record 1: insert successfully
    Record 2: insert successfully
    Record 3: insert successfully
    Record 4: Update Failed
    .
    .
    .
    Record 100: insert successfully


    Snippets of my code:


    BusinessFacadeImpl.java

    Code:
    public class BusinessFacadeImpl implments BusinessFacade {
            private BusinessDao businessDao;
    
            public BusinessDao setBusinessDao(BusinessDao businessDao) {
                 this.businessDao = businessDao;
            }
    
    	void batchUpdate() {
                 try {
                   businessDao.batchUpdate();
    
                 } catch (Throwable t) {
                    throw new ApplicationException(t);  
                 }
             }
    }
    application-context.xml

    Code:
    	<bean id="baseTx" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"><ref bean="transactionManager"/></property>
    		<property name="target"><ref local="codewordTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="batch*">PROPAGATION_REQUIRED,+ApplicationException</prop>
    			</props>
    		</property>
    	</bean>
    This can only force commit for my record 1, 2 and 3 and stop proceeding after record 4. What I really want is to ignore the exception in record 4 and proceed to inserting/updating for the rest of my records. I have tried various ways like "PROPAGATION_NEVER" or renaming the method but still in vain. Please kindly advise me on this. Thanks.

  • #2
    Is ApplicationException checked or unchecked. If it's checked, then you don't even need the +ApplicationException you have, since checked exceptions by default get ignnored for commit/rollback. If it's unchecked, then it's needed. But in either case, the code calling this method needs to handle the exception, so the loop can continue. What's happening in your case in that code? When you say it stops, what does that really mean?

    And how are you doing the batch really. I am assuming you are calling the batchUpdate method 100 times. But if instead you are relying on iBatis's batch feature, then you are out of luck. It's iBatis that is giving up if somewhere in the batch a piece of it fails...

    Comment


    • #3
      My ApplicationException is a unchecked exception. You are right, I am using a loop with IBatis's executeBatch feature. When I say stop, I mean the execution gets out of my businessDao and is caught by the Throwable statement in my BusinessFacadeImpl batchUpdate method.

      If I interprete you correctly, you are telling me to do a try and catch exception in my businessDao instead and handle the exception by my own way?

      i.e:
      Code:
      public class BusinessSqlMapDao implements BusinessDao &#123;
         public void batchUpdate&#40;&#41; &#123;
             ...
             executor.startBatch&#40;&#41;;
      
            for &#40;int i = 0; i < 100; i++&#41; &#123;
                try &#123;
                   ....
                    executor.executeBatch&#40;&#41;;
                &#125; catch &#40;ApplicationException e&#41; &#123;
                    log.info&#40;"Record " + i + "&#58; " + e&#41;;
                &#125;
      
            &#125;
      
         &#125;
      
      &#125;

      Comment


      • #4
        Well you can't use iBatis's batch feature at all. There's no way (that I know of) for you to tall it to keep on going after part of a batch fails, since the whole point is that it sends it all as a batch to the JDBC driver. So what I am saying is to just loop yourself to do your batch, and feed iBatis teh batch pieces one by one. Obviously this is not going to be as efficient as doing a real batch...

        Comment

        Working...
        X