orientdb 和 waterline 中的事务 [英] Transaction in orientdb and waterline
问题描述
我正在尝试在 waterline 中创建事务,但我从 OrientDB 收到此错误:
I am trying to to create transaction in waterline but I am getting this error from OrientDB:
com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException: Cannot find a command executor for the command request: sql.BEGIN
这是我的代码:
try {
itemsModel.query("BEGIN", function(err) { if (err) {throw new Error(err);}
itemsModel.update({id:items_ids,status:ACTIVE},{status:INACTIVE})
.exec(function(err, INA_items){ if (err) {throw new Error(err);}
if (INA_items.length != items_ids.length ) {throw new Error({err:RECORD_NOT_FOUND});}
itemsModel.query("COMMIT", function(err) { if (err) {throw new Error({err:MSG.RECORD_NOT_FOUND,title:"ITEMS"});} });
});
});
}
catch(e){
itemsModel.query("ROLLBACK", function(err) {
if (err) {return res.serverError(err);}
return res.serverError(e);
});
}
我也直接在orientdb中检查了BEGIN
命令,但同样的错误.
I also checked BEGIN
command directly in orientdb, but same error.
推荐答案
目前的问题是混合了几个不同的概念,我将尝试一一解决:
The question as it stands is mixing several different concepts, I'll try to address one by one:
Waterline 的 API 本身不支持事务(检查 issue #755);
看起来您正在使用 sails-orientdb
适配器,并从中执行一个 raw SQL 查询;
It looks like you are using sails-orientdb
adapter and from it you are executing a raw SQL query;
您在示例的第二行中执行的 SQL 查询只是 BEGIN
,这就是 OrientDB 本身抛出错误的地方.事务 SQL 查询必须类似于:
The SQL query you are executing in the second line of your example is just BEGIN
and that's where OrientDB itself is throwing an error. A transaction SQL query must be something like:
begin
let account = create vertex Account set name = 'Luke'
let city = select from City where name = 'London'
let edge = create edge Lives from $account to $city
commit retry 100
return $edge
示例取自 OrientDB 文档.
或者,您可以通过使用 javascript 风格 使用事务>Oriento 数据库对象.假设您使用的是 sails-orientdb
,您可以像这样获得 Oriento DB 对象:
Alternatively you can use transactions in a more javascript style by making use of the Oriento DB object. Assuming you are using sails-orientdb
you can get the Oriento DB object like this:
var db = itemsModel.getDB();
// using oriento syntax, you can do something like
var tx = db.begin();
tx.create({
'@class': 'TestClass',
name: 'item3'
});
return tx.commit()
.then(function (results) {
console.log(results.created.length); // should be 1
});
示例取自 Oriento 测试.另一个很好的例子可以在 Oriento 的示例文件夹中找到.>
Example taken from Oriento tests. Another good example can be found at Oriento's example folder.
这篇关于orientdb 和 waterline 中的事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!