Announcement Announcement Module
Collapse
No announcement yet.
One transaction per job Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • One transaction per job

    Hi guys,

    We use spring-batch 1.0.1. We don't need retry/skip/reset functionality. What we need is to to run all job within one transaction, i.e., all or nothing gets saved in DB.


    Code:
    	<aop:config>
    		<aop:pointcut id="batchJob" expression="execution(* org.springframework.batch.core.job.SimpleJob..*(..))" />
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="batchJob" />
    	</aop:config>	
    
    	<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
        	<tx:attributes>
    			<tx:method name="execute" propagation="REQUIRED" />
        	</tx:attributes>
    	</tx:advice>
    Unfortunately this doesn't work correctly, updates and inserts are performed on DB on every processed item. If exception occurs, only last operation is rollbacked, but strange is that from log I see that only one transaction is used so everything should be rollbacked.

    For example in this case exception occurs afterStep, but update SQL is fired and item is updated. Excerpt from the log:
    Code:
    2013-06-10 14:32:46,873 8946 [Thread-1] DEBUG o.s.o.h.HibernateTransactionManager - Creating new transaction with name [org.springframework.batch.core.Job.execute]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
    2013-06-10 14:32:46,881 8954 [Thread-1] DEBUG o.h.transaction.JDBCTransaction - begin
    2013-06-10 14:32:46,882 8955 [Thread-1] DEBUG o.h.transaction.JDBCTransaction - current autocommit status: true
    2013-06-10 14:32:46,882 8955 [Thread-1] DEBUG o.h.transaction.JDBCTransaction - disabling autocommit
    Hibernate:
        update
            item
        set
            posted=?
        where
            item_id=?
    2013-06-10 14:32:48,308 10381 [Thread-1] ERROR o.h.e.d.AbstractFlushingEventListener - Could not synchronize database state 
    2013-06-10 14:32:48,308 10381 [Thread-1] ERROR o.s.b.c.step.item.ItemOrientedStep - Fatal error detected during commit.
    2013-06-10 14:32:48,309 10382 [Thread-1] ERROR o.s.batch.core.step.AbstractStep - Encountered an error executing the step
    2013-06-10 14:32:48,314 10387 [Thread-1] DEBUG o.h.transaction.JDBCTransaction - rollback
    2013-06-10 14:32:48,315 10388 [Thread-1] DEBUG o.h.transaction.JDBCTransaction - re-enabling autocommit
    2013-06-10 14:32:48,315 10388 [Thread-1] DEBUG o.h.transaction.JDBCTransaction - rolled back JDBC Connection

    I even tried to use "ResourcelessTransactionManager" for "org.springframework.batch.core.step.item.SimpleSt epFactoryBean", that didn't help.

    What I'm missing? Is it even possible with spring-batch 1.0.1?
    Last edited by maris; Jun 10th, 2013, 08:30 AM.
Working...
X