0

我有一个 Java 代码,用于从文本文件在数据库中插入许多 sql 行。我使用 setAutoCommit(false) 对连接进行了编程,最后如果没有发生错误(检测到所有方法都抛出 Throwable),我发送提交。该任务通常需要 30 分钟。

它在电缆连接中效果很好,但在 wifi 连接中它永远不会到达终点,因为连接有时会在短时间内丢失。

为了解决它。我编写了两件事:将文本文件的行转换为具有 ArrayList 中所有行的序列化对象,并创建其他具有 int 索引的序列化对象,以保存成功插入的最后一行的索引。

然后,在程序中我这样做:

charge in memory the object.lines, from the serialized object.
charge in memory the object.index, from the serialized object.

伪代码:

loop:
index = sum 1 to the object.index
line = object.getLine(index)
insert line
if error continue (or goto loop)
send commit
if error continue (or goto loop)
object.index = index
serialize object

这样我就有了成功提交到数据库的行的备份,我可以在其他时间继续工作。如果我在一行中遇到连接问题,我可以尝试再次插入该行。

如果我有连接问题,我会等待 1 分钟。连接已恢复,但会自动重置,而不是由我重置。

然后例如在这样的行中:

INSERT INTO my_table1 (id) VALUES (sq_mytable1_id.NEXTVAL);
//success

//connection lost
//connection reset

INSERT INTO my_table2 (id) VALUES (sq_mytable1_id.CURRVAL);
//error, sq_mytable1_id.CURRVAL is not in session.

我收到 ORA-08002 异常,因为连接已重置,我可以从会话中获取 sq_mytable1_id.CURRVAL。

拜托,你能告诉我如何编程一个批处理 sql 插入器来容忍 wi-fi 中的连接中断吗?

我认为序列化连接,但我不能:oracle.jdbc.driver.T4CConnection 不可序列化。

4

0 回答 0