2015-10-15 90 views
0

我正在用多線程在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(); 

然後我打電話的功能添加頂點

多個線程訪問功能添加頂點這需要輸入參數 - 隨機生成的唯一編號(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:無法持久性

回答

0

期間,由於異常提交事務如果此代碼:

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

是你addVertex功能的內部和多線程呼籲addVertex ,我不認爲你正在使用newTransaction很對。這創建了所謂的「線程交易」。線程事務是多個線程在SAME事務上作用的事務。在您的使用中,每個線程正在創建自己的不同事務。鑑於你所描述的東西,我會說,正確的方法是使addVertex是:

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

我還要考慮刪除鎖定(除非你需要它)。