Flush interceptor in batch executor type

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Flush interceptor in batch executor type

Daniel Kvasnička
Hi all,

in our application, we are trying to implement optimistic locking via mybatis interceptor 

We want to use mybatis in batch mode (mybatis.executor-type=batch).   

In our business logic we actually simply call update DAO method that call sql UPDATE statement.  

We defined interceptor like this:
@Intercepts({@Signature(type = Executor.class, method = "flushStatements", args = {})})

Executor.flushStatements method is internally called every time the transaction is commited. So there is no need (and we don't want to do it) to call flush method explicitly. When Executor.flushStatements is called internally from Executor.commit then this call is not invoked on proxy (Plugin) and there is no way how to intercept flushStatements method and get results (List<BatchResult>) of our update methods to resolve optimistic locking.

If we call @Flush annotated method explicitly from our business logic, interceptor works right as well as our optimistic locking implementation. But this would mean we have to call dao.flush() method every time we call dao.update() method. And we don't want to .....

Please is there any way how to intercept Executor.flushStatements method in batch mode?
Actually we solved the problem adding our custom FlushResultHandler (similar as mybatis ResultSetHandler, StatementHandler, ...) to mybatis.

To Configuration class we add:

 public FlushResultHandler newFlushHandler(Executor executor, List<BatchResult> flushResults) {

    FlushResultHandler flushResultHandler = new DefaultFlushResultHandler(executor, flushResults);

    flushResultHandler = (FlushResultHandler) interceptorChain.pluginAll(flushResultHandler);

    return flushResultHandler;


and to doFlushStatements method in BatchExecutor before return line (return results;) we added these lines: 

final FlushResultHandler resultSetHandler = configuration.newFlushHandler(this, results);



Our Interceptor looks like this:


@Intercepts({@Signature(type = FlushResultHandler.class, method = "handleResults", args = {})})

public class OptimisticLockingInterceptor implements Interceptor {

Before I ask for pull request of this our feature, I would like to ask mybatis community if there is some way how to solve this problem within actual mybatis mechanisms.

Thank you very much

You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/e412b02d-b41e-4094-8d17-2a079e94cdd8n%40googlegroups.com.