翻译整理:纪玉奇
 
几乎所有与JanusGraph的交互都是通过Transaction,JansuGraph的Transaction支持并发。使用Transaction时,不需要显式进行生命,graph.V()即会开启一个事务,graph.commit()则会提交一个事务。用户也可以使用graph.newTransaction()获取对事务的控制权。
另外,事务不一定是ACID,而是依赖于后端支持情况,如果是BerkleyDB则可以,但不被Cassandra和HBase支持。

Transaction Handling

对JanusGraph的每个操作都是在事务执行,无需显式声明,由第一次操作开启。
graph = JanusGraphFactory.open("berkeleyje:/tmp/janusgraph")
juno = graph.addVertex() //Automatically opens a new transaction
juno.property("name", "juno")
graph.tx().commit() //Commits transaction

graph = JanusGraphFactory.open("berkeleyje:/tmp/janusgraph")
juno = graph.addVertex() //Automatically opens a new transaction
juno.property("name", "juno")
graph.tx().commit() //Commits transaction
 
事务在commit()或数据库shutdown()时才结束。

Transaction Scope

vertices, edges和type都在transaction scope的范围中,在Tinpop的默认语法,transaction是自动创建的,一旦提交和关闭,元素则不可用。
对于vertex来说,JanusGraph将会自动创建一个vertices并交由一个新的transaction scope,用户就无需显式声明了。
graph = JanusGraphFactory.open("berkeleyje:/tmp/janusgraph")
juno = graph.addVertex() //Automatically opens a new transaction
graph.tx().commit() //Ends transaction
juno.property("name", "juno") //Vertex is automatically transitioned

graph = JanusGraphFactory.open("berkeleyje:/tmp/janusgraph")
juno = graph.addVertex() //Automatically opens a new transaction
graph.tx().commit() //Ends transaction
juno.property("name", "juno") //Vertex is automatically transitioned
对于Edge,不会自动创建新的transaction,而且无法在初始transaction之外访问,需要显式创建:

Transaction Failures

当提交事务时,由于各种原因,往往不会成功,如因为网络问题,IO问题等。实际上,对于大型系统,事务将会最终失败。
try {
if (g.V().has("name", name).iterator().hasNext())
throw new IllegalArgumentException("Username already taken: " + name)
user = graph.addVertex()
user.property("name", name)
graph.tx().commit()
} catch (Exception e) {
//Recover, retry, or return error message
println(e.getMessage())
}
如果事务执行失败,将会抛出JanusGraphException,失败原因有很多种,可以分为如下两类:
  • Potentially temporary failures
由于IO,网络原因导致的失败,重试可能成功,且由JanusGraph自动重试,在配置其重试次数。
  • Permanent failures
产生于完全的网络断开或锁。

Multi-Threaded Transactions

JanusGraph通过tinkerpop的transactions支持multi-threaded transaction,通过多线程可以充分利用多核CPU。要开启多线程事务,需要使用createThreadedTx()方法。
threadedGraph = graph.tx().createThreadedTx();
threads = new Thread[10];
for (int i=0; i<threads.length; i++) {
threads[i]=new Thread({
println("Do something with 'threadedGraph''");
});
threads[i].start();
}
for (int i=0; i<threads.length; i++) threads[i].join();
threadedGraph.tx().commit();
Concurrent Algorithms
当实现并发图算法时经常使用createThreadedTx()方法。

Nested Transactions 嵌套事务

当在一个事务中逻辑较长时,占有锁的时间也较长,很有可能发生竞争。如下面的情况:
v1 = graph.addVertex()
//Do many other things
v2 = graph.addVertex()
v2.property("uniqueName", "foo")
v1.addEdge("related", v2)
//Do many other things
graph.tx().commit() // This long-running tx might fail due to contention on its uniqueName lock
针对此种情况,可以采用在一个短的,嵌套的、线程无关的事务中创建一个顶点,如下示:
v1 = graph.addVertex()
//Do many other things
tx = graph.tx().createThreadedTx()
v2 = tx.addVertex()
v2.property("uniqueName", "foo")
tx.commit() // Any lock contention will be detected here
v1.addEdge("related", g.V(v2).next()) // Need to load v2 into outer transaction
//Do many other things
graph.tx().commit() // Can't fail due to uniqueName write lock contention involving v2

Common Transaction Handling Problems

事务不用手动启动,需要手动启动的只有multi-threaded transaction。
事务是由Tinkerpop的语句自动启动的,但是需要显式的关闭,这种操作很有必要,因为只有使用者知道事务的范围。完成一个事务需要执行g.commit()或g.rollback(),事务将试图从一开始就维护状态,而这很可能导致问题。
v = g.V(4).next() // Retrieve vertex, first action automatically starts transaction
g.V(v).bothE()
>> returns nothing, v has no edges
//thread is idle for a few seconds, another thread adds edges to v
g.V(v).bothE()
>> still returns nothing because the transactional state from the beginning is maintained
从上面的代码中可以看出,由于事务没有结束,对象的状态得意维持,另外线程的更改没有体现出来。这个问题通常出现在客户端-服务器模式部署的场景下,为了解决此问题,用户需要在执行完毕后手工调用commit()方法。
v = g.V(4).next() // Retrieve vertex, first action automatically starts transaction
g.V(v).bothE()
graph.tx().commit()
//thread is idle for a few seconds, another thread adds edges to v
g.V(v).bothE()
>> returns the newly added edge
graph.tx().commit()
在使用multi-threaded事务时,在事务范围内创建的所有的vertices和edges在事务外均不可见,在事务结束后访问这些元素将会导致错误。根据上面展示的,这些element需要在新事务中显示刷新,使用:
g.V(existingVertex)或者g.E(existingEdge)

Transaction Configruation

JanusGraph.buildTransaction()方法给了用户设置和启动multi-threaded transaction的能力,也可以通过JanusGraph.newTransaction()进行设置。
 
  • readOnly()
  • enableBatchLoading()
  • setTimestamp()
  • setVertexCacheSize(long)
  • checkExternalVertexExistence(boolean)
  • checkInternalVertexExistence(boolean)
  • consistencyChecks(boolean)

JanusGraph中的事务的更多相关文章

  1. Microsoft SQL Server中的事务与并发详解

    本篇索引: 1.事务 2.锁定和阻塞 3.隔离级别 4.死锁 一.事务 1.1 事务的概念 事务是作为单个工作单元而执行的一系列操作,比如查询和修改数据等. 事务是数据库并发控制的基本单位,一条或者一 ...

  2. Redis系列之key操作命令与Redis中的事务详解(六)

    序言 本篇主要目的有二: 1.展示所有数据类型中key的所有操作命令,以供大家学习,查阅,更深入的挖掘redis潜力. 2.掌握redis中的事务,让你的数据完整性一致性拥有更优的保障. redis命 ...

  3. SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因

    原本打算写有关 SSIS Package 中的事务控制过程的,但是发现很多基本的概念还是需要有 SQL Server 事务和事务的隔离级别做基础铺垫.所以花了点时间,把 SQL Server 数据库中 ...

  4. 【转】SQL Server中的事务与锁

    SQL Server中的事务与锁   了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂 ...

  5. 存储过程中使用事务,sql server 事务,sql事务

    一.存储过程中使用事务的简单语法       在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 ...

  6. 【MySQL】漫谈MySQL中的事务及其实现

    最近一直在做订单类的项目,使用了事务.我们的数据库选用的是MySQL,存储引擎选用innoDB,innoDB对事务有着良好的支持.这篇文章我们一起来扒一扒事务相关的知识. 为什么要有事务? 事务广泛的 ...

  7. SQL Server中的事务日志管理(9/9):监控事务日志

    当一切正常时,没有必要特别留意什么是事务日志,它是如何工作的.你只要确保每个数据库都有正确的备份.当出现问题时,事务日志的理解对于采取修正操作是重要的,尤其在需要紧急恢复数据库到指定点时.这系列文章会 ...

  8. Spring中的事务

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  9. SQL2005中的事务与锁定(八)- 转载

    ------------------------------------------------------------------------ -- Author : happyflystone - ...

随机推荐

  1. 【OpenJudge9277】【递推】Logs Stacking堆木头

    Logs Stacking堆木头 总时间限制: 1000ms 内存限制: 131072kB [描述] Daxinganling produces a lot of timber. Before loa ...

  2. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  3. nand flash坏块管理OOB,BBT,ECC

    转:http://www.cnblogs.com/elect-fans/archive/2012/05/14/2500643.html 0.NAND的操作管理方式 NAND FLASH的管理方式:以三 ...

  4. 那些年我们踩过的坑-NSTimer

    昨天下午工作的时候遇见一个这样的需求,网络请求失败后把请求数据保存到本地,并自动重发3次,时间间隔是10秒,如果3次后还失败的话,下一次启动这个接口的时候,把新数据和保存在本地的数据都要发送,刚开始以 ...

  5. 【redis】在spring boot2.0中使用redis的StringRedisTemplate 自动注入@Autowired

    1.使用opv.increment 达到增量的效果[判断某个用户 是第几次做这种操作] @RequestMapping("createCode") @RestController ...

  6. fiddler抓取手机上https数据配置和失败的解决办法

    1. 设置fiddler,Tools-Options...      抓取https的话,勾选红框中的内容 2. fiddler默认监听端口8888 3. 查看本机IP 4. 打开手机 设置-无线局域 ...

  7. JAVA常见算法题(二十八)

    package com.forezp.util; import java.util.Arrays; /** * 两个int数组,都是从小到大的的排列,请合并为一个新的数组,也是从小到到大的排列, * ...

  8. jquery动态添加表单数据

    动态添加用户 实现代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html ...

  9. ylbtech-LanguageSamples-Delegates(委托)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Delegates(委托) 1.A,示例(Sample) 返回顶部 “委托”示例 本示例 ...

  10. JSP和Servlet中的几个编码的作用及原理

    首先,说说JSP和Servlet中的几个编码的作用. 在JSP和Servlet中主要有以下几个地方可以设置编码,pageEncoding="UTF-8".contentType=& ...