0
votes

I am creating a vertices in Titan with multiple threads. Here's my code

Trying to connect to Titan, Specifying the schema and then Adding vertices.

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();

Then I'm calling a function to Add Vertex

Multiple threads accessing function Add Vertex which takes input parameter - randomly generated unique numbers (entityPK)

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

I read that the exception that is thrown is because of not specifying the LOCK. But even after specifying LOCK, exception is thrown.

com.thinkaurelius.titan.core.TitanException: Could not commit transaction due to exception during persistence

1

1 Answers

0
votes

If this code:

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

is inside of your addVertex function and multiple threads are calling addVertex, I don't think you're using newTransaction quite right. That creates what is referred to as a "threaded transaction". A threaded transaction is one where multiple threads act on the SAME transaction. In your usage, each thread is creating its own DIFFERENT transaction. Given what you've described, I would say that the correct approach would be to make addVertex be:

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

I would also consider dropping locking (unless you need it).