Announcement Announcement Module
Collapse
No announcement yet.
@Transactional in Spring Batch Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Transactional in Spring Batch

    Hello,

    Are there any thoughts/guidelines on the use of @Transactional within a Spring Batch managed context?

    What I'm trying to implement is a @OnWriteError logger as follows:

    Code:
     public class WriteErrorListener {
    
        private final String LOG_EXCEPTION_STATEMENT = "insert into ERROR_LOGGER (id, exception) values(null, ?)";
    
        private SimpleJdbcTemplate jdbcTemplate;
    
        public WriteErrorListener(SimpleJdbcTemplate jdbcTemplate) {
    	this.jdbcTemplate = jdbcTemplate;
        }
    
        @OnWriteError
        public void onWriteError(Exception exception, List<Work> works) {
    	jdbcTemplate.update(LOG_EXCEPTION_STATEMENT, exception.toString());
        }
    }
    }
    However, in case of a write error Spring Batch will naturally rollback the transaction including the above db call.
    My initial thought was to specify a @Transactional(propagation=Propagation.REQUIRES_NE W) annotation on the WriteErrorListener class but I keep reading that mixing the @Transactional annotation with Spring Batch's transactions is a bad idea and could cause unpredictable results.

    Any input from the committers? Apart from using plain JDBC (Connection object, Statement etc) are there any other better (and more declarative) ways of committing that error log entry in case of a rollback?

    Cheers,
    Savvas

  • #2
    Spring batch will rollback the chunk where the error occurs and go for one by one item processing to get bad record. I think what ever you have written inside onWriteError will not rollback, since jdbcTemplate starts its own transaction.

    We have sucessfully implemented by calling one manager class in side onWriteError method of listener (with @Transactional(propagation=Propagation.REQUIRES_NE W)). It is sucessfully logging the records into DB.

    Comment

    Working...
    X