【问题标题】:Multithreading in Titan throws exceptionTitan中的多线程抛出异常
【发布时间】:2015-10-15 05:34:14
【问题描述】:

我正在用多个线程在 Titan 中创建一个顶点。 这是我的代码

尝试连接到 Titan,指定架构,然后添加顶点。

g = TitanFactory.open("/conf/titan-cassandra.properties");
TitanManagement mgmt = g.getManagementSystem();
final PropertyKey userid = mgmt.makePropertyKey("userid").dataType(Integer.class).make();
TitanGraphIndex namei = mgmt.buildIndex("userid",Vertex.class).addKey(userid).unique().buildCompositeIndex();
mgmt.setConsistency(namei, ConsistencyModifier.LOCK);
mgmt.commit();

然后我调用一个函数来添加顶点

多线程访问函数Add Vertex,它接受输入参数-随机生成的唯一数字(entityPK)

tx1 = g.newTransaction();
Vertex user_ver = tx1.addVertexWithLabel("user");
user_ver.setProperty("userid",entityPK);
//Adding other properties for vertex
tx1.commit();`

我读到抛出的异常是因为没有指定 LOCK。但是即使指定了LOCK,也会抛出异常。

com.thinkaurelius.titan.core.TitanException: 由于持久化期间出现异常而无法提交事务

【问题讨论】:

    标签: java titan


    【解决方案1】:

    如果这个代码:

    tx1 = g.newTransaction();
    Vertex user_ver = tx1.addVertexWithLabel("user");
    user_ver.setProperty("userid",entityPK);
    //Adding other properties for vertex
    tx1.commit();
    

    在您的addVertex 函数内部,并且多个线程正在调用addVertex,我认为您使用newTransaction 不太正确。这创建了所谓的“线程事务”。线程事务是多个线程作用于同一事务的事务。在您的使用中,每个线程都在创建自己的不同事务。鉴于您所描述的,我想说正确的方法是使addVertex成为:

    Vertex user_ver = g.addVertexWithLabel("user");
    user_ver.setProperty("userid",entityPK);
    //Adding other properties for vertex
    g.commit();
    

    我也会考虑放弃锁定(除非你需要它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      相关资源
      最近更新 更多