Midnight Crash: The ‘Impossible’ MyBatis‑Plus Pitfalls That Can Break Production

The article catalogs six production‑ready pitfalls of MyBatis‑Plus—duplicate Snowflake IDs, batch‑insert disorder, enum storage errors, camel‑case mismatches, auto‑fill failures, and JSON field loss—explains their root causes with concrete examples and code, and provides practical solutions to keep the framework reliable in high‑concurrency environments.

dbaplus Community
dbaplus Community
dbaplus Community
Midnight Crash: The ‘Impossible’ MyBatis‑Plus Pitfalls That Can Break Production

Background and ORM Choice

Rapid data growth and micro‑service fragmentation have pushed Java teams to seek an ORM that balances speed and stability. MyBatis‑Plus emerged as a production‑oriented enhancement over raw MyBatis, offering CRUD shortcuts, logical deletion, auto‑fill, optimistic locking, multi‑tenant support, and code generation.

Production Challenges

In development environments the framework appears flawless, but once deployed the hidden defaults surface, leading to critical failures such as primary‑key conflicts and data inconsistency.

1. Snowflake ID Duplication

Problem: Two container instances generated identical Snowflake IDs, causing primary‑key collisions at 3 AM.

Root Causes:

Machine ID not configured, all instances use the default.

Docker containers clone MAC addresses; the framework extracts the last two bytes and mods by 32, causing collisions when instances exceed 32.

Kubernetes pods receive virtual MACs from CNI plugins, often identical or unavailable, leading to the same workerId.

Clock rollback duplicates timestamps, dataCenterId, workerId, and sequence.

Solution: Provide a unique distributed ID service or switch to database‑generated IDs.

@Slf4j
@Component
public class IdHelper {
    public static long getIdc() {
        try {
            return ZZIdcClient.getInstance().get();
        } catch (Throwable e) {
            log.error("act=getIdc error={}", e.getMessage());
            log.info("k=s act=getIdcByIDHelper");
            return IDHelper.generator(0);
        }
    }
}

Alternative enum for IdType shows built‑in options:

public enum IdType {
    AUTO(0), NONE(1), INPUT(2), ASSIGN_ID(3), ASSIGN_UUID(4),
    @Deprecated ID_WORKER(3), @Deprecated ID_WORKER_STR(3), UUID(4);
    private final int key;
}
MyBatis‑Plus machine ID strategy
MyBatis‑Plus machine ID strategy
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.

JavaORMMyBatis-PlusBatch InsertSnowflake IDauto fillenum mappingcamel caseJSON storage
dbaplus Community
Written by

dbaplus Community

Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.

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.