简体   繁体   English

是否可以使用Java API确定“节点”是否在Neo4J中的事务中?

[英]Is it possible to determine if a `Node` is in a transaction in Neo4J using the Java API?

Is it possible to determine if a Node is in a transaction? 是否可以确定Node是否在事务中? It is possible to get a GraphDatabaseService by the method Node.getGraphDatabase . 这是可能得到一个GraphDatabaseService该方法Node.getGraphDatabase

I would like to do something like this: 我想做这样的事情:

public class Neo4JHelper {
    public void setProperty(Node node, String key, Object value) {
        if(isInTransaction(node) {
            node.setProperty(key, value);
        } else {
            throw new MyOwnException("You are trying to set a node outside a transaction... you suck");
        }
    }

    private boolean isInTransaction(Node node) {
        //Something
    }
}

The reason I want to do this is because I would like to give my users a custom error when trying to use my class Neo4JHelper outside a transaction. 我要这样做的原因是,在尝试在事务外使用我的类Neo4JHelper时,我想给用户一个自定义错误。

Another solution would be if it is possible to somehow tell the compiler that you need a transaction to use the method/class and otherwise give a compile error. 另一个解决方案是,是否有可能以某种方式告诉编译器您需要使用该方法/类的事务,否则会产生编译错误。

I have several different takes on this, I'm not sure which one is most helpful. 我对此有几种不同的看法,我不确定哪一种最有帮助。

Point #1 is that you don't need to do this checking. 第一点是您不需要执行此检查。 If you attempt to create any dummy node, and you're not inside of a transaction, the DB will throw an exception indicating that you're not in a transaction. 如果您尝试创建任何虚拟节点,并且您不在事务内部,那么数据库将抛出异常,表明您不在事务中。 So if you wanted to detect this situation, just try to create a trivial test node. 因此,如果您想检测这种情况,只需尝试创建一个简单的测试节点即可。 No exception? 没有例外? You're in a transaction. 您正在交易中。

Point #2 is maybe you're asking if a particular node is in a transaction. 第二点可能是您在询问某个特定节点是否在事务中。 I'm not aware of any way to do that, maybe a developer can add something on that. 我不知道有什么办法,也许开发人员可以在上面添加一些东西。 Within transactions, it's possible to acquire a read/write lock on an individual node. 在事务内,可以在单个节点上获取读/写锁定。 So if you had the Transaction object (which your method doesn't) then a surrogate method might be to determine whether there's a lock on a given node. 因此,如果您有Transaction对象(您的方法没有),则替代方法可能是确定给定节点上是否有锁。

Point #3 while I'm not 100% sure what you're doing, your code suggest there's a different way of going about this problem so you don't even have to answer this question. 第3点虽然我不是100%不确定您在做什么,但是您的代码表明有解决此问题的另一种方法,因此您甚至不必回答这个问题。 If you want your users to get a custom error, go ahead and try to modify the property -- if neo4j throws an exception that you're not in a transaction, again, there's your answer. 如果您希望用户遇到自定义错误,请继续尝试修改该属性-如果neo4j抛出您不在事务中的异常,同样,您也可以找到答案。 Catch that exception, then throw your custom error. 捕获该异常,然后引发自定义错误。

A node cannot be in a transaction, only a current execution (Thread) can be. 节点不能处于事务中,只能是当前执行(线程)。

There is an internal way to check for a running transaction: 有一种内部方法可以检查正在运行的事务:

   ThreadToStatementContextBridge txManager = ((GraphDatabaseAPI) graphDB).getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class);
   txManager.hasTransaction();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM