Orient DB中的事务管理

时间:2016-08-24 05:24:14

标签: java orientdb graph-databases

我正在使用以下逻辑在Orient DB中启动事务。     graphFactory.getDatabase().begin(type); 我观察到的是如果事务没有在方法内部关闭,它可以自动用于其他方法,这似乎泄漏给我。我将尝试解释下面的情况:

graphFactory.getDatabase().begin(type);

我有另一种方法,如更新

 public <T> void addVertexToDB(T data){
    graphFactory.getDatabase().begin(OTransaction.TXTYPE.OPTIMISTIC);
    //logic for fetching data and adding vertex
    //I am neither calling rollback nor commit here
    }

图表工厂的属性集

public <T> void updateVertexToDB(T data){
    //not starting the transaction explicitly and doing some write operation
    //logic for fetching data and adding vertex
    //I am neither calling rollback nor commit here
    }

期望:updateVertexToDB应抛出graphFactory.setAutoStartTx(false); graphFactory.setRequireTransaction(true); 当前行为:它通过保存和正常工作来启动交易。

所以我只想知道如何实现如果一个方法没有开始转换并尝试写入DB的行为,它必须抛出OTransactionException e,直到它没有嵌套在已经运行的事务的方法中。

2 个答案:

答案 0 :(得分:1)

什么是graphFactory?这个名字具有误导性。它看起来不像OrientGraphFactory,而是一个OrientGraph对象,对吗?

无论如何,通过电话graphFactory.getDatabase().begin(OTransaction.TXTYPE.OPTIMISTIC);,您将绕过GraphAPI。如果您使用Graph API,行为应该是预期的行为。尝试拨打graphFactory.begin();

答案 1 :(得分:0)

我认为这是我验证交易的方式。我在同一个Junit类中编写了这两个方法测试,它由同一个线程(main)运行。因此交易是共享的。 我通过在第二种方法中生成新线程并尝试写入数据库并且抛出异常来交叉验证。