Coder Social home page Coder Social logo

Comments (6)

aozarov avatar aozarov commented on July 22, 2024

Yes, the reason that we didn't make Transaction Closable is because close does not have enough context (as oppose to Python with clause) about success/failure of the operation.
I think having the need to explicitly commit but rather automatically rollback (if not committed) is not intuitive/expected and I would like to get some more feedback/opinions before going ahead with it.
For now we provided the DatastoreService#runInTransaction convenient method.

from google-cloud-java.

eamonnmcmanus avatar eamonnmcmanus commented on July 22, 2024

I agree with Arie that it would be surprising for Transaction.close() to mean "abort the transaction if it is not committed". What you would want instead would be for it to mean "commit the transaction unless there was an exception, in which case abort it". But unfortunately there's no way for the close() method to know whether it is being invoked because of an exception or because execution of the try block completed normally.

I think I would be in favour of having only the DatastoreService.runInTransaction method so that it is impossible to accidentally abandon a transaction due to an exception. The argument to TransactionCallable would then need to be a Transaction, which is probably better than a DatastoreReaderWriter anyway? The resulting code would be slightly more verbose on Java 7 or before, but quite succinct on Java 8 with lambdas:

result = dataStore.runInTransaction((t) -> {
  t.query()...t.put()...;
  return something;
});

from google-cloud-java.

aozarov avatar aozarov commented on July 22, 2024

I agree that passing Transaction instead of DatastoreReaderWriter to DatastoreService.runInTransaction sounds nicer but it has the side effect of also including a way
to manually/explicitly commit or rollback (the main 2 methods that Transaction adds to DatastoreReaderWriter).

Though having an explicit commit/rollback option in the callback could be seen as advantage/feature, I feel that it may lead to more confusing user code (where the callback code can't tell for sure if it is still active) and thought we should better avoid it.

Few possible options:

  1. Leave it as is.
  2. Replace callback param with a new TransactionCallback interface (similar to Transaction but without a way to explicitly commit/rollback).
  3. Replace callback param with Transaction (and allow callback to explicitly commit/rollback).

Thoughts?

from google-cloud-java.

jboynes avatar jboynes commented on July 22, 2024

You need transactions to fail safe i.e. roll back by default. The user must specifically commit the work and know that the commit succeeded so that other work can rely on it. The nasty path is where the VM itself raises an Error (which can happen at any time) which must result the work getting rolled back.

The simplest way to ensure that behaviour is to have the user explicitly commit their work. The user can then assume that any error that prevented the commit operation completing successfully resulted in no work being applied.

For reference, where the JDBC spec says it is "implementation-defined" (doh!) whether Connection#close() commits or rolls back, the drivers from MySQL, Postgres, DB2 (non-mainframe) and SQLServer all roll back by default whereas only Oracle and DB2 (mainframe) commit. Similarly a UserTransaction in JavaEE will always rollback unless it is specifically committed.

from google-cloud-java.

jboynes avatar jboynes commented on July 22, 2024

Based on @eamonnmcmanus comment, Java 8's lambdas would make this intuitive:

interface DataStore {
  /** Simple way to run work in a transaction. */
  <ResultT> ResultT runInTransaction(Supplier<ResultT> work);

  /** Run work in a transaction with ability to manually control outcome. */
  <ResultT> ResultT runInTransaction(Function<Transaction, ResultT> work);
}

The Transaction interface would only need to add one method for transaction control:

interface Transaction extends DatastoreReaderWriter {
  void setRollbackOnly();
}

which would would like the same method in javax.transaction.UserTransaction

Work would be automatically committed if the function returned normally, or would be rolled back if the block threw a Throwable. Per Java8 functional APIs, the work would not be able to throw a checked Exception.

To run this on Java 7, we can backport the Supplier and Function interfaces. I realize equivalents exist in Guava but I suggest we avoid using Guava classes in the API.

from google-cloud-java.

garrettjonesgoogle avatar garrettjonesgoogle commented on July 22, 2024

This has been added to our feature backlog: https://github.com/GoogleCloudPlatform/google-cloud-java/wiki/Feature-backlog . This issue will be closed but is linked in the backlog and can continue to be used for comment and discussion.

from google-cloud-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.