oracle Java JDBC clearBatch() 和堆内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2138319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:52:36  来源:igfitidea点击:

Java JDBC clearBatch() and heap memory

javaoraclememoryjdbcbatch-processing

提问by Firone

I've noticed the following behavior.

我注意到以下行为。

I have a file that is about 3MB containing several thousand rows. In the rows I split and create prepared statement (about 250 000 statements).

我有一个大约 3MB 的文件,其中包含几千行。在行中,我拆分并创建了准备好的语句(大约 250 000 个语句)。

What I do is:

我要做的是:

preparedStatement
addBatch
do for every 200 rows {
 executeBatch
 clearBatch().
}

at the end

在末尾

commit()

The memory usage will increase to around 70mb without out of memory error. Is it possible get the memory usage down? and have the transactional behavior (if one fails all fails.). I was able to lower the memory by doing commit with the executeBatchand clearBatch... but this will cause a partial insert of the total set.

内存使用量将增加到 70mb 左右,而不会出现内存不足错误。是否有可能降低内存使用率?并具有事务性行为(如果失败,则全部失败。)。我能够通过使用executeBatchand进行提交来降低内存clearBatch......但这会导致总集的部分插入。

回答by stacker

You could insert all rows into a temp table with same structure and if everything is fine. let the database insert them into to target table using: insert into target (select * from temp). In case the import into the temp table fails you haven't changed anything in you target table.

如果一切正常,您可以将所有行插入具有相同结构的临时表中。让数据库使用: 将它们插入到目标表中insert into target (select * from temp)。如果导入临时表失败,则您没有更改目标表中的任何内容。

EDIT: fixed syntax

编辑:固定语法

回答by Timothy

You could also use the JDBC 2.0 "batch processing" feature.

您还可以使用 JDBC 2.0“批处理”功能。

  1. Set your dbconnection using connection.setAutoCommit(false)
  2. Add batches to your statement using statement.addBatch(sql_text_here)
  3. Once your batches are all loaded, execute it using: statement.executeBatch()
  4. Commit it using connection.commit()
  5. Catch exceptions and rollback as necessary using connection.rollback()
  1. 使用设置您的 dbconnection connection.setAutoCommit(false)
  2. 使用 statement.addBatch(sql_text_here)
  3. 一旦您的批次都加载完毕,请使用以下命令执行它: statement.executeBatch()
  4. 使用它提交 connection.commit()
  5. 捕获异常并根据需要使用回滚 connection.rollback()

More on exception handling for rollback... here is a typical rollback exception handler:

有关回滚异常处理的更多信息……这是一个典型的回滚异常处理程序:

  catch( BatchUpdateException bue )
  {
    bError = true;
    aiupdateCounts = bue.getUpdateCounts();

    SQLException SQLe = bue;
    while( SQLe != null)
    {
      // do exception stuff

      SQLe = SQLe.getNextException();
    }
  } // end BatchUpdateException catch
  catch( SQLException SQLe )
  {
    ...

  } // end SQLException catch

Read up here: http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html#JDBC2015

在这里阅读:http: //java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html#JDBC2015