0
public class a {
    private static TitanGraph titanGraph = null;
    static GraphTraversalSource traversalSource = null;
public static void main(String a[])
{
    titanGraph = TitanFunctions.getTitanGraph();
    traversalSource = titanGraph.traversal();
    // Task to be executed by each thread
    Runnable r = new Runnable() {
        public void run() {

            long ab = traversalSource.V().has("RequestJob", "RequestId", 203)
                    .has("JobLockStatus","F")
                    .property("JobLockStatus", "B").count().next();
            titanGraph.tx().commit();
            System.out.println("value: "+ab+" : " +Thread.currentThread().getName());
        }
    };
    Thread t1 = new Thread(r, "T1");
    Thread t2 = new Thread(r, "T2");
    t1.start();
    t2.start(); 
    }}

In the above program, Two concurrent threads are trying to update the value..from “F” to “B” for the same RequestId=203.
It seems like both threads are getting the status value as “F” and updating it to B. But I want only one thread should change the value from “F” to “B”. After updating the value , I have used commit() also to save the changes.
If any thread changed the status from “(F)ree” to “(B)usy”..and other thread should see the changed value..(“B”).
Kindly help me to resolve this.

User12345
  • 455
  • 2
  • 10
  • 21

1 Answers1

2

You could use a mutex or a synchronized() block to ensure only one thread can perform that operation at any given time. Something like the following may work:

titanGraph = TitanFunctions.getTitanGraph();
traversalSource = titanGraph.traversal();
Runnable r = new Runnable() {
    public void run() {
        synchronized(titanGraph){
            long ab = traversalSource.V().has("RequestJob", "RequestId", 203)
                .has("JobLockStatus","F")
                .property("JobLockStatus", "B").count().next();
            titanGraph.tx().commit();
        }    
    }
};
Thread t1 = new Thread(r, "T1");
Thread t2 = new Thread(r, "T2");
t1.start();
t2.start(); 

So the above block synchronized(titanGraph) is basically stating that for whatever is inside that block it can only executed by a thread that has the lock on the object within the brackets. In this case the titanGraph. If a thread does not have the lock then it waits for the lock to become available.

Here is a really nice tutorial on using synchronized

Filipe Teixeira
  • 3,565
  • 1
  • 25
  • 45
  • @Filipe..It is working..thank u for the link and explanation....one more doubt is ..if the processes run across nodes..and more than one process try to change the status.only one process among them should change the `joblockstatus`. How can I achieve this? Kindly help me. checked `ConsistencyModifier.Lock` in titan database..but it is not working.. – User12345 Mar 24 '17 at 06:24
  • As long as the above code runs on a single JVM (which it will) titan will handle the eventual consistency side itself. As specified [here](http://s3.thinkaurelius.com/docs/titan/1.0.0/eventual-consistency.html) The `ConsistencyModifier.Lock` is useful if you want to be more sure of your uniqueness constraints. It slows down commits but ensure unique indices are maintained. – Filipe Teixeira Mar 24 '17 at 14:47