Class DatastoreTransactionManager

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void assertInTransaction()
      Throws IllegalStateException if the caller is not in a transaction.
      void clearSessionCache()
      Clears the session cache if the underlying database is Datastore, otherwise it is a no-op.
      <T> QueryComposer<T> createQueryComposer​(java.lang.Class<T> entity)
      Returns a QueryComposer which can be used to perform queries against the current database.
      void delete​(VKey<?> key)
      Deletes the entity by its id.
      void delete​(java.lang.Iterable<? extends VKey<?>> vKeys)
      Deletes the set of entities by their key id.
      void delete​(java.lang.Object entity)
      Deletes the given entity from the database.
      void deleteWithoutBackup​(VKey<?> key)
      Deletes the entity by its id without writing commit logs if the underlying database is Datastore.
      void deleteWithoutBackup​(java.lang.Iterable<? extends VKey<?>> keys)
      Deletes the set of entities by their key id without writing commit logs if the underlying database is Datastore.
      void deleteWithoutBackup​(java.lang.Object entity)
      Deletes the given entity from the database without writing commit logs if the underlying database is Datastore.
      <R> R doTransactionless​(java.util.function.Supplier<R> work)
      Executes the work in a transactionless context.
      <T> boolean exists​(VKey<T> key)
      Returns whether the entity of given key exists.
      boolean exists​(java.lang.Object entity)
      Returns whether the given entity with same ID exists.
      org.joda.time.DateTime getTransactionTime()
      Returns the time associated with the start of this particular transaction attempt.
      void insert​(java.lang.Object entity)
      Persists a new entity in the database, throws exception if the entity already exists.
      void insertAll​(com.google.common.collect.ImmutableCollection<?> entities)
      Persists all new entities in the database, throws exception if any entity already exists.
      void insertAllWithoutBackup​(com.google.common.collect.ImmutableCollection<?> entities)
      Persists all new entities in the database without writing any backup if the underlying database is Datastore.
      void insertWithoutBackup​(java.lang.Object entity)
      Persists a new entity in the database without writing any backup if the underlying database is Datastore.
      boolean inTransaction()
      Returns true if the caller is in a transaction.
      boolean isOfy()
      Returns true if the transaction manager is DatastoreTransactionManager, false otherwise.
      <T> com.google.common.collect.ImmutableList<T> loadAllOf​(java.lang.Class<T> clazz)
      Returns a stream of all entities of the given type that exist in the database.
      <T> com.google.common.collect.ImmutableList<T> loadByEntities​(java.lang.Iterable<T> entities)
      Loads all given entities from the database.
      <T> com.google.common.collect.ImmutableList<T> loadByEntitiesIfPresent​(java.lang.Iterable<T> entities)
      Loads all given entities from the database if possible.
      <T> T loadByEntity​(T entity)
      Loads the given entity from the database.
      <T> T loadByKey​(VKey<T> key)
      Loads the entity by its key.
      <T> java.util.Optional<T> loadByKeyIfPresent​(VKey<T> key)
      Loads the entity by its key, returns empty if the entity doesn't exist.
      <T> com.google.common.collect.ImmutableMap<VKey<? extends T>,​T> loadByKeys​(java.lang.Iterable<? extends VKey<? extends T>> keys)
      Loads the set of entities by their keys.
      <T> com.google.common.collect.ImmutableMap<VKey<? extends T>,​T> loadByKeysIfPresent​(java.lang.Iterable<? extends VKey<? extends T>> keys)
      Loads the set of entities by their keys.
      void put​(java.lang.Object entity)
      Persists a new entity or update the existing entity in the database.
      void putAll​(com.google.common.collect.ImmutableCollection<?> entities)
      Persists all new entities or updates the existing entities in the database.
      void putAll​(java.lang.Object... entities)
      Persists all new entities or updates the existing entities in the database.
      void putAllWithoutBackup​(com.google.common.collect.ImmutableCollection<?> entities)
      Persists all new entities or update the existing entities in the database without writing commit logs if the underlying database is Datastore.
      void putWithoutBackup​(java.lang.Object entity)
      Persists a new entity or update the existing entity in the database without writing commit logs if the underlying database is Datastore.
      static <T> T toSqlEntity​(T obj)
      Converts an object to the corresponding SqlEntity if necessary and possible.
      void transact​(java.lang.Runnable work)
      Executes the work in a transaction.
      <T> T transact​(java.util.function.Supplier<T> work)
      Executes the work in a transaction and returns the result.
      void transactNew​(java.lang.Runnable work)
      Pauses the current transaction (if any) and executes the work in a new transaction.
      <T> T transactNew​(java.util.function.Supplier<T> work)
      Pauses the current transaction (if any), executes the work in a new transaction and returns the result.
      void transactNewReadOnly​(java.lang.Runnable work)
      Executes the work in a read-only transaction.
      <R> R transactNewReadOnly​(java.util.function.Supplier<R> work)
      Executes the work in a read-only transaction and returns the result.
      void update​(java.lang.Object entity)
      Updates an entity in the database, throws exception if the entity does not exist.
      void updateAll​(com.google.common.collect.ImmutableCollection<?> entities)
      Updates all entities in the database, throws exception if any entity does not exist.
      void updateAllWithoutBackup​(com.google.common.collect.ImmutableCollection<?> entities)
      Updates all entities in the database without writing any backup if the underlying database is Datastore.
      void updateWithoutBackup​(java.lang.Object entity)
      Updates an entity in the database without writing commit logs if the underlying database is Datastore.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DatastoreTransactionManager

        public DatastoreTransactionManager​(Ofy injectedOfy)
        Constructs an instance.
    • Method Detail

      • inTransaction

        public boolean inTransaction()
        Description copied from interface: TransactionManager
        Returns true if the caller is in a transaction.

        Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.

        Specified by:
        inTransaction in interface TransactionManager
      • assertInTransaction

        public void assertInTransaction()
        Description copied from interface: TransactionManager
        Throws IllegalStateException if the caller is not in a transaction.

        Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.

        Specified by:
        assertInTransaction in interface TransactionManager
      • transact

        public <T> T transact​(java.util.function.Supplier<T> work)
        Description copied from interface: TransactionManager
        Executes the work in a transaction and returns the result.
        Specified by:
        transact in interface TransactionManager
      • transactNew

        public <T> T transactNew​(java.util.function.Supplier<T> work)
        Description copied from interface: TransactionManager
        Pauses the current transaction (if any), executes the work in a new transaction and returns the result.

        Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.

        Specified by:
        transactNew in interface TransactionManager
      • transactNew

        public void transactNew​(java.lang.Runnable work)
        Description copied from interface: TransactionManager
        Pauses the current transaction (if any) and executes the work in a new transaction.

        Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.

        Specified by:
        transactNew in interface TransactionManager
      • transactNewReadOnly

        public <R> R transactNewReadOnly​(java.util.function.Supplier<R> work)
        Description copied from interface: TransactionManager
        Executes the work in a read-only transaction and returns the result.

        Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.

        Specified by:
        transactNewReadOnly in interface TransactionManager
      • transactNewReadOnly

        public void transactNewReadOnly​(java.lang.Runnable work)
        Description copied from interface: TransactionManager
        Executes the work in a read-only transaction.

        Note that this function is kept for backward compatibility. We will review the use case later when adding the cloud sql implementation.

        Specified by:
        transactNewReadOnly in interface TransactionManager
      • getTransactionTime

        public org.joda.time.DateTime getTransactionTime()
        Description copied from interface: TransactionManager
        Returns the time associated with the start of this particular transaction attempt.
        Specified by:
        getTransactionTime in interface TransactionManager
      • insert

        public void insert​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Persists a new entity in the database, throws exception if the entity already exists.
        Specified by:
        insert in interface TransactionManager
      • insertAll

        public void insertAll​(com.google.common.collect.ImmutableCollection<?> entities)
        Description copied from interface: TransactionManager
        Persists all new entities in the database, throws exception if any entity already exists.
        Specified by:
        insertAll in interface TransactionManager
      • insertWithoutBackup

        public void insertWithoutBackup​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Persists a new entity in the database without writing any backup if the underlying database is Datastore.

        This method is for the sake of keeping a single code path when replacing ofy() with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.

        Specified by:
        insertWithoutBackup in interface TransactionManager
      • insertAllWithoutBackup

        public void insertAllWithoutBackup​(com.google.common.collect.ImmutableCollection<?> entities)
        Description copied from interface: TransactionManager
        Persists all new entities in the database without writing any backup if the underlying database is Datastore.

        This method is for the sake of keeping a single code path when replacing ofy() with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.

        Specified by:
        insertAllWithoutBackup in interface TransactionManager
      • put

        public void put​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Persists a new entity or update the existing entity in the database.
        Specified by:
        put in interface TransactionManager
      • putAll

        public void putAll​(java.lang.Object... entities)
        Description copied from interface: TransactionManager
        Persists all new entities or updates the existing entities in the database.
        Specified by:
        putAll in interface TransactionManager
      • putAll

        public void putAll​(com.google.common.collect.ImmutableCollection<?> entities)
        Description copied from interface: TransactionManager
        Persists all new entities or updates the existing entities in the database.
        Specified by:
        putAll in interface TransactionManager
      • putWithoutBackup

        public void putWithoutBackup​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Persists a new entity or update the existing entity in the database without writing commit logs if the underlying database is Datastore.

        This method is for the sake of keeping a single code path when replacing ofy() with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.

        Specified by:
        putWithoutBackup in interface TransactionManager
      • putAllWithoutBackup

        public void putAllWithoutBackup​(com.google.common.collect.ImmutableCollection<?> entities)
        Description copied from interface: TransactionManager
        Persists all new entities or update the existing entities in the database without writing commit logs if the underlying database is Datastore.

        This method is for the sake of keeping a single code path when replacing ofy() with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.

        Specified by:
        putAllWithoutBackup in interface TransactionManager
      • update

        public void update​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Updates an entity in the database, throws exception if the entity does not exist.
        Specified by:
        update in interface TransactionManager
      • updateAll

        public void updateAll​(com.google.common.collect.ImmutableCollection<?> entities)
        Description copied from interface: TransactionManager
        Updates all entities in the database, throws exception if any entity does not exist.
        Specified by:
        updateAll in interface TransactionManager
      • updateWithoutBackup

        public void updateWithoutBackup​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Updates an entity in the database without writing commit logs if the underlying database is Datastore.

        This method is for the sake of keeping a single code path when replacing ofy calls with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.

        Specified by:
        updateWithoutBackup in interface TransactionManager
      • updateAllWithoutBackup

        public void updateAllWithoutBackup​(com.google.common.collect.ImmutableCollection<?> entities)
        Description copied from interface: TransactionManager
        Updates all entities in the database without writing any backup if the underlying database is Datastore.

        This method is for the sake of keeping a single code path when replacing ofy calls with tm() in the application code. When the method is invoked with Datastore, it won't write the commit log backup; when invoked with Cloud SQL, it behaves the same as the method which doesn't have "WithoutBackup" in its method name because it is not necessary to have the backup mechanism in SQL.

        Specified by:
        updateAllWithoutBackup in interface TransactionManager
      • exists

        public boolean exists​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Returns whether the given entity with same ID exists.
        Specified by:
        exists in interface TransactionManager
      • loadByKeysIfPresent

        public <T> com.google.common.collect.ImmutableMap<VKey<? extends T>,​T> loadByKeysIfPresent​(java.lang.Iterable<? extends VKey<? extends T>> keys)
        Description copied from interface: TransactionManager
        Loads the set of entities by their keys.

        Nonexistent keys / entities are absent from the resulting map, but no NoSuchElementException will be thrown.

        Specified by:
        loadByKeysIfPresent in interface TransactionManager
      • loadByEntitiesIfPresent

        public <T> com.google.common.collect.ImmutableList<T> loadByEntitiesIfPresent​(java.lang.Iterable<T> entities)
        Description copied from interface: TransactionManager
        Loads all given entities from the database if possible.

        Nonexistent entities are absent from the resulting list, but no NoSuchElementException will be thrown.

        Specified by:
        loadByEntitiesIfPresent in interface TransactionManager
      • loadByKeys

        public <T> com.google.common.collect.ImmutableMap<VKey<? extends T>,​T> loadByKeys​(java.lang.Iterable<? extends VKey<? extends T>> keys)
        Description copied from interface: TransactionManager
        Loads the set of entities by their keys.
        Specified by:
        loadByKeys in interface TransactionManager
      • loadByEntities

        public <T> com.google.common.collect.ImmutableList<T> loadByEntities​(java.lang.Iterable<T> entities)
        Description copied from interface: TransactionManager
        Loads all given entities from the database.
        Specified by:
        loadByEntities in interface TransactionManager
      • loadAllOf

        public <T> com.google.common.collect.ImmutableList<T> loadAllOf​(java.lang.Class<T> clazz)
        Description copied from interface: TransactionManager
        Returns a stream of all entities of the given type that exist in the database.

        The resulting stream is empty if there are no entities of this type. In Datastore mode, if the class is a member of the cross-TLD entity group (i.e. if it has the InCrossTld annotation, then the correct ancestor query will automatically be applied.

        Specified by:
        loadAllOf in interface TransactionManager
      • delete

        public void delete​(java.lang.Iterable<? extends VKey<?>> vKeys)
        Description copied from interface: TransactionManager
        Deletes the set of entities by their key id.
        Specified by:
        delete in interface TransactionManager
      • delete

        public void delete​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Deletes the given entity from the database.
        Specified by:
        delete in interface TransactionManager
      • deleteWithoutBackup

        public void deleteWithoutBackup​(java.lang.Iterable<? extends VKey<?>> keys)
        Description copied from interface: TransactionManager
        Deletes the set of entities by their key id without writing commit logs if the underlying database is Datastore.
        Specified by:
        deleteWithoutBackup in interface TransactionManager
      • deleteWithoutBackup

        public void deleteWithoutBackup​(java.lang.Object entity)
        Description copied from interface: TransactionManager
        Deletes the given entity from the database without writing commit logs if the underlying database is Datastore.
        Specified by:
        deleteWithoutBackup in interface TransactionManager
      • clearSessionCache

        public void clearSessionCache()
        Description copied from interface: TransactionManager
        Clears the session cache if the underlying database is Datastore, otherwise it is a no-op.
        Specified by:
        clearSessionCache in interface TransactionManager
      • isOfy

        public boolean isOfy()
        Description copied from interface: TransactionManager
        Returns true if the transaction manager is DatastoreTransactionManager, false otherwise.
        Specified by:
        isOfy in interface TransactionManager
      • toSqlEntity

        public static <T> T toSqlEntity​(@Nullable
                                        T obj)
        Converts an object to the corresponding SqlEntity if necessary and possible.

        This should be used when returning objects from Datastore to make sure they reflect the most recent type of the object in question.