【发布时间】:2017-06-04 01:55:45
【问题描述】:
我正在开发工具,以不断地将更改从 MongoDb 导出到 Oracle 数据库。
我在执行批处理操作(Oracle)时遇到问题。
static void save(List result) {
withBatchConnection { Statement stm ->
result.each { String line ->
stm.addBatch(line)
}
}
}
static withConnection(Closure closure) {
def conn = null
boolean success = false
while (!success) {
try {
conn = getConnection()
closure.call(conn)
success = true
} catch (e) {
log.error('Connection problem', e)
log.error(e, e)
log.info('Retrying for 30 sec')
sleep(30000)
} finally {
conn?.close()
}
}
}
static withTransactionConnection(Closure closure) {
withConnection { Sql sql ->
OracleConnection conn = sql.getConnection() as OracleConnection
conn.setAutoCommit(false)
closure.call(conn)
conn.commit()
}
}
static withBatchConnection(Closure closure) {
withTransactionConnection { Connection conn ->
def statement = conn.createStatement()
closure.call(statement)
statement.executeBatch()
statement.close()
}
}
问题是我不能使用准备好的语句,因为操作的顺序非常重要。
当我使用 Rewrite Batched Statements 保存到 MySql 时,它每秒执行 10k 次操作。对于 Oracle 是 400 次操作/秒
有没有机会让它更快?
我正在使用 OJDBC 7 和 groovy 2.4.7
【问题讨论】:
-
Oracle 和 MySQL 的一个重要区别是 Oracle 中事务的“提交”阶段,而 MySQL 中不存在该阶段。可能是你观察到的来源。每一步之后是否有提交到您的数据库?如果是这样,请避免它,并提交每个例如10000 次更新。
-
我看不出准备好的语句和操作顺序之间有什么关系
-
检查您的连接是否默认将自动提交设置为“开启”,这意味着在每个语句之后进行提交。
-
@john16384 在准备好的语句中,您只能执行相同类型的操作,例如插入同一张表。在我的情况下,有:插入 a,插入 b,从 a 中删除;插入;
标签: java oracle groovy batch-processing ojdbc