本文介绍了如何在远程 OrientDB 上使用事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

TL;DR从 Java 服务器使用远程 OrientDB 数据库时,如何在事务中执行 SQL 命令?

TL;DRHow do I make SQL commands execute in a transaction when using a remote OrientDB database from a java server?

长版

我有一个从 Java 服务器连接到的远程 OrientDB 数据库.

I have a remote OrientDB database that I connect to from a java server.

我在 OrientDB 文档中读到要启动一个事务我调用 db.begin() 并且在数据库更新后我调用 db.commit()db.rollback().

I read in the OrientDB documentation that to start a transaction I call db.begin() and after the database updates I call db.commit() or db.rollback().

所以这就是我最初想要做的:

So this is initially what I was trying to do:

try {
  db.begin();
  db.command(new OCommandSQL('delete edge connected from #10:1')).execute();
  db.command(new OCommandSQL('create edge connected from #10:1 to BROKEN_SQL')).execute();
  db.commit();
} catch (Throwable e) {
  db.rollback();
}

那没有用.它删除了边缘(在提交之前).按预期在创建边缘线上抛出异常但没有回滚.然后我在文档中阅读

That didn't work. It deleted the edge (before it was commit). Threw an exception on the create edge line as expected but did not rollback. I then read in the documentation that

NOTE: OrientDB keeps the transaction on client RAM

客户端内存;这意味着在调用 db.commit() 之前,数据库服务器完全不知道 java 服务器在做什么.

client RAM; Meaning that the database server is completely unaware of what the java server is doing until db.commit() is called.

这不是我遇到的情况,当单步执行代码时,每个命令确实在服务器上执行,db.begin()db.rollback()没有任何影响.然后我读了

That is not what happened to me and when stepping through the code each command is indeed executed on the server and db.begin() and db.rollback() has no effect whatsoever. Then I read

SQL commands are always executed on the server side.
They don't care if you're in the middle of a transaction on the client side!

好的!这就解释了.所以我试试这个

OK! That explains it. So I try this

try {
  db.command(new OCommandSQL('begin')).execute();
  db.command(new OCommandSQL('delete edge connected from #10:1')).execute();
  db.command(new OCommandSQL('create edge connected from #10:1 to BROKEN_SQL')).execute();
  db.command(new OCommandSQL('commit')).execute();
} catch (Throwable e) {
  db.command(new OCommandSQL('rollback')).execute();
}

立即失败:

Request processing failed; nested exception is com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException:
Cannot find a command executor for the command request: sql.begin

然而,我使用 db.save(o)db.delete(o) 和事务取得了成功.一切似乎都与文档一致.但是如何确保我的 SQL 命令 在事务中完成.我不在乎交易是在客户端还是服务器上.我已经尝试过 OrientDB 2.1.13、2.1.15 和 2.1.25.

I do however have success using db.save(o) or db.delete(o) and transactions. Everything appears to align with documentation. But how do I make sure my SQL commands are done in a transaction. I do not care if the transaction is on the client or server. I've tried with OrientDB 2.1.13, 2.1.15 and 2.1.25.

推荐答案

我找到了一个可能的解决方案.通过一些帮助课程,它可能会扩大规模.

I found a possible solution. With some help classes it might be possible to get it to scale.

StringBuilder query = new StringBuilder();
query.append("begin\n");
things.forEach((thing) -> {
  query.append("delete edge owner from " + thing.getId() + "\n");
  query.append("create edge owner from " + thing.getId() + " to " + newOwner.getId() + "\n");
});
query.append("commit\n");

try {
  db.command(new OCommandScript(query)).execute()
} catch (Throwable t) {
  logger.error(t.toString());
}

这篇关于如何在远程 OrientDB 上使用事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 15:02