Should graph traversals we explicitly closed?


woj...@...
 

Hi,

See the following code:

    GraphTraversalSource graphTraversalSource = graph.traversal();

   
// (1) one liner - no warnings
   
Vertex src = graphTraversalSource.V().has(ID, id).next();

   
// (2) assignment to variable generates a warning: potential resource leak: GraphTraversal is AutoClosable and should be managed by try-with-resources
   
GraphTraversal<Vertex, Vertex> g = graphTraversalSource.V().has(ID, id);  
   
Vertex src = g.next();

Question 1: Should we ignore the warning in (2) or wrap each traversal with try-with-resources?

The tinkerpop documentation
http://tinkerpop.apache.org/docs/current/reference
does not say much about resource management of traversals, only that transactions and graph instances should be explicitly closed.

Question 2: Is is safe to reuse single graphTraversalSource for the lifetime of an application?

The javadoc for graph.traversal states that instances of GraphTraversalSource are reusable, so following this we are keeping a single source for spawning the traversals for each request that comes to our web-app.
At the same time we are not closing the spawned traversals (question 1). Is this correct approach or will this lead to resource/memory leaks?
Should we spawn we source for each traversal or keep one common instance for all of them?

Any help of this topic would be much appreciated.

Thanks





Jason Plurad <plu...@...>
 

> Question 1: Should we ignore the warning in (2) or wrap each traversal with try-with-resources?

I'd think you should follow the recommendation. There was another thread recently (I don't remember which forum) where the results of a traversal were remaining in memory on the Gremlin Server because the traversal wasn't fully iterated. You also might want to consider checking hasNext() or using tryNext() on that traversal, otherwise you'll get an exception if there is no matching id.

> Question 2: Is is safe to reuse single graphTraversalSource for the lifetime of an application?

Yes. Creating the graphTraversalSource could be considered an expensive operation, so it should be reused. One thing you need to be aware of is ensuring that you handle the transactions cleanly. Make sure the traversals are surrounded with try/catch/finally and graph.tx().commit() or graph.tx().rollback() at the end.


On Monday, August 7, 2017 at 8:46:20 AM UTC-4, wojcik.w wrote:
Hi,

See the following code:

    GraphTraversalSource graphTraversalSource = graph.traversal();

   
// (1) one liner - no warnings
   
Vertex src = graphTraversalSource.V().has(ID, id).next();

   
// (2) assignment to variable generates a warning: potential resource leak: GraphTraversal is AutoClosable and should be managed by try-with-resources
   
GraphTraversal<Vertex, Vertex> g = graphTraversalSource.V().has(ID, id);  
   
Vertex src = g.next();

Question 1: Should we ignore the warning in (2) or wrap each traversal with try-with-resources?

The tinkerpop documentation
http://tinkerpop.apache.org/docs/current/reference
does not say much about resource management of traversals, only that transactions and graph instances should be explicitly closed.

Question 2: Is is safe to reuse single graphTraversalSource for the lifetime of an application?

The javadoc for graph.traversal states that instances of GraphTraversalSource are reusable, so following this we are keeping a single source for spawning the traversals for each request that comes to our web-app.
At the same time we are not closing the spawned traversals (question 1). Is this correct approach or will this lead to resource/memory leaks?
Should we spawn we source for each traversal or keep one common instance for all of them?

Any help of this topic would be much appreciated.

Thanks