Why MyBatis Returns -2147482646 with BATCH Executor and How to Fix It

The article explains that MyBatis returns the constant -2147482646 when the executor type is set to BATCH, shows how to trace the source code to uncover this behavior, and provides a simple configuration change to restore correct update counts.

Programmer DD
Programmer DD
Programmer DD
Why MyBatis Returns -2147482646 with BATCH Executor and How to Fix It

Issue

When running a demo project the author noticed that MyBatis update and insert operations always returned the value -2147482646, regardless of the actual result.

Root Cause

The problem is caused by the defaultExecutorType being set to BATCH. In batch mode MyBatis discards the update count, and the BatchExecutor returns the constant -2147482646.

Fix

Change the executorType in the Spring configuration to SIMPLE (or remove the argument). Example bean definition:

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    <!-- executor type -->
    <constructor-arg name="executorType" value="SIMPLE"/>
</bean>

After this change the update/insert methods return the correct affected‑row count.

Source Tracing

The author opened the MyBatis source code to follow the call chain:

Configuration loads conf.xml and builds a SqlSessionFactory. SqlSessionFactory.openSession() creates a SqlSession (interface with insert and update methods).

The default implementation is DefaultSqlSession, which delegates to an Executor.

If executorType is BATCH, the BatchExecutor is used; its update method returns the constant -2147482646.

When executorType is SIMPLE, SimpleExecutor calls Statement.getUpdateCount(), returning the actual number of rows affected.

Conclusion

Changing the executor type from BATCH to SIMPLE resolves the mysterious return value, and the article demonstrates the importance of tracing source code to understand unexpected framework behavior.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentspringMyBatisBatchExecutorType
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.