"Does OrientGraphFactory.getTx() will return the same OrientGraph instance for current thread"
Yes, because it's using OPartitionedDatabasePool <http://orientdb.com/javadoc/latest/com/orientechnologies/orient/core/db/OPartitionedDatabasePool.html> However, be careful about two gotchas: 1) By default there is always an active TX. When you obtain an instance from factory there is a TX. When you commit, there is immediately created a new TX as well. This does not go well with TX propagation, because you want to start TX/increment nesting counter at the beginning of each method and commit or decrement counter at the end of each method. So you will probably need to do this: graphFactory.setAutoStartTx(false); 2) OrientGraph.begin() does not increment TX nesting counter. <https://github.com/orientechnologies/orientdb/issues/5067> Use: graph.getRawGraph().begin(); instead of graph.begin() (commit and rollback should work fine) Here is an example of helper method for your transactional propagable methods: public void tx(AtomicGraphOperation op, int maxRetries) { this.log.info("max retries: "+String.valueOf(maxRetries)); if (maxRetries < 0) { throw new IllegalArgumentException("Argument retries must be >= 0"); } for (int retry = 0; retry == 0 || (retry <= maxRetries && !(this.graph. getRawGraph().getTransaction().isActive())); ++retry) { // We want to retry only outermost tx if (retry > 0) { this.log.info("Retrying TX for "+retry+" time"); } try { this.graph.getRawGraph().begin(); // OrientGraph.begin() does not increment TX nesting counter op.perform(this.graph); this.graph.commit(); break; // ok, cancel retrying } catch (ONeedRetryException e) { this.graph.getRawGraph().rollback(); this.log.debug("TX needs retry", e); // continue retrying } catch (Exception e) { this.graph.getRawGraph().rollback(); throw e; // rethrow } } } usage: public void doSomething() { tx((graph) -> { // do something in propagated TX }); } Dne čtvrtek 10. března 2016 20:38:24 UTC+1 Tai Hu napsal(a): > > Hi, > I am a little confused about how to handle transaction propagation in > OrientGraph correctly. Basically I want to make group of method calls into > one transaction. Based on OrientDB documentation, I have to use the same > instance of OrientGraph object to achieve this. In my code, each method is > using the following method call to get OrientGraph instance > > OrientGraphFactory.getTx(); > > OrientGraphFactory is a singleton in my application and configured as new > OrientGraphFactor("plocal:data").setupPool(1, 10); > > Now my question is "Does OrientGraphFactory.getTx() will return the same > OrientGraph instance for current thread? If not, does that mean my > transaction is not propagated? Do I have to implement my own ThreadLocal to > hold on to the same OrientGraph instance for current thread or pass the > same OrientGraph instance into every single method? > > Thanks, > > Tai > -- --- You received this message because you are subscribed to the Google Groups "OrientDB" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
