You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate操作DB2:插入300万条数据到jvald_address后自动删除求助

Troubleshooting Auto-Deleted 3M Records After Insertion via Hibernate

Hey there! Losing 3 million records right after inserting them is no fun—let’s break down the most likely culprits and fixes based on your scenario and Hibernate’s behavior:

1. Missing Transaction Commit (The #1 Culprit)

Looking at your code snippet, you’ve started a Transaction tx but didn’t show the full flow. If you forget to call tx.commit() after inserting data, Hibernate will automatically roll back the transaction when the Session is closed. This is super common with openSession() (unlike getCurrentSession() which might auto-commit depending on config).

Double-check your code:

Transaction tx = session.beginTransaction();
// Insert logic here
tx.commit(); // This line is critical!

Also, make sure you’re not swallowing exceptions that would trigger a rollback. Wrap your logic in a try-catch to explicitly roll back on failure and log errors properly.

2. Using Session Instead of StatelessSession for Bulk Inserts

You commented out StatelessSession and used Session—that’s a problem for 3M records. Session maintains a first-level cache of all persisted objects, which will quickly consume all your memory. When the JVM hits an out-of-memory error, the transaction will silently roll back, making it look like data was deleted.

StatelessSession is designed for bulk operations: it doesn’t cache objects, flushes immediately, and is way more memory-efficient. Switch back to it with this pattern:

public String getaddress() {
    StatelessSession session = sessionFactory.openStatelessSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        // Your bulk insert logic (e.g., looping through ADDRESS records)
        for (Address sourceAddr : sourceAddressList) {
            JvaldAddress targetAddr = mapToJvaldAddress(sourceAddr);
            session.insert(targetAddr);
        }
        tx.commit();
        return "Insertion successful";
    } catch (Exception e) {
        if (tx != null) tx.rollback();
        e.printStackTrace(); // Replace with proper logging in production!
        return "Insertion failed: " + e.getMessage();
    } finally {
        if (session != null) session.close();
    }
}

3. Accidental Deletion via Hibernate Query/Listeners

If you’re using HQL queries for insertion, double-check that you didn’t mix up INSERT with DELETE syntax (easy typo!). Also, check if you have any Hibernate interceptors or event listeners (like PostInsertEventListener) that run after insertion and accidentally delete the new records.

Enable Hibernate’s DEBUG logging to see exactly what SQL statements are being executed—this will reveal if a delete is being triggered unexpectedly.

4. Database-Level Triggers

Don’t rule out the database itself! If the jvald_address table has an AFTER INSERT trigger that executes a DELETE statement, your data will vanish right after insertion, regardless of Hibernate.

Test this directly in the database: run a manual INSERT query and check if the record stays. If it gets deleted, you’ve found your trigger issue.

5. Concurrent Transactions or External Processes

Is there another service, cron job, or batch process that deletes data from jvald_address? Check your database logs for DELETE statements around the time of your insertion. It’s possible your insert completes, but another process immediately removes the records.


内容的提问来源于stack exchange,提问作者Praque Manohar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:40:42