Video in progress
For an ingestion service that receives thousands of records per second, do you stay on Hibernate or move to JDBC/jOOQ? How do you decide?
I measure first: with SEQUENCE + batch_size + order_inserts + driver rewrite, Hibernate handles a lot of load. If the profile shows the cost of the persistence context, hydration and dirty checking, or if I need upsert/COPY, I move only that write path to JDBC, and the rest of the domain stays on Hibernate.
📖 Answer
This isn't an "either/or" question. I usually go with a hybrid, but based on numbers.
Step 1: configure Hibernate properly and measure:
SEQUENCEid,allocationSize50+;hibernate.jdbc.batch_size50-100,order_inserts=true;reWriteBatchedInserts=trueon PostgreSQL;StatelessSession, orflush/clearafter every N records.
Often this alone raises throughput several times over, and that's where the discussion ends.
Step 2: where is the time going? I look with async-profiler or JFR. If the CPU is in ActionQueue, EntityEntry, TypeHelper.deepCopy, that's ORM overhead. If the time is in the database (WAL, indexes, locks), moving to JDBC won't help, and you need to look at the schema and indexes.
Step 3: things the ORM can't do:
insert ... on conflict do update(upsert): Hibernate 6.5+ partially supportsMERGE, but you don't get full control;- PostgreSQL
COPY: the fastest route for millions of rows; - bulk in a guaranteed single round trip.
I move such a path into a separate IngestionWriter (JdbcTemplate or jOOQ), while the domain rules and the read side stay on Hibernate.
The cost: two kinds of persistence code, manually invalidating the L2 cache if there is one, and maintaining @Version and audit fields yourself. That's why I choose this route only when a real bottleneck is proven, and I verify in a test that both paths give the same result.
Sign up to mark studied questions and save your progress.